API Dingo Api
数据库/表转为migragtion/seed Xethron/migrations-generator
路由分组
group 方法使用prefix
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['namespace' => 'App\Http\Controllers'], function ($api) {
$api -> group(['prefix'=>'loveq'], function() use ($api){
//节目列表
$api -> get('program', 'Loveq@index');
});
});
返向数据迁移
根据现有数据表,生成php代码,写在model中
空白
获取相关路径
根目录路径
app() -> basePath()
根目录下的app路径
app('path')
增加自定义函数helpers.php
-
新建目录:
/app/Http/Helpers/
,哪一级目录不存在则新建 -
在刚新建的Heplers目录下新建文件:
CommonHelpers.php
-
新建Provider
/app/Providers/
目录下新建HelperServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register() {
foreach (glob(app('path').'/Http/Helpers/*.php') as $filename) {
require_once($filename);
}
}
}
- app.php里加载Helper Provider
/bootstrap/app.php
文件添加一个register$app -> register(App\Providers\HelperServiceProvider::class);
获取所有config:
app()->make('config')
返回一个json
response() -> json($res)
查询 所有文章和分类
<?php
namespace App\Http\Controllers;
use App\Model\Blog;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
class Blog extends BaseController
{
/**
* @description index
* @author liaozhiwei
* @email itime2@qq.com
**/
function showPost() {
$res = Blog\Content()::with("meta") -> select(['cid','title','created','type','status','commentsNum']) -> paginate(15);
return response() -> json($res);
}
自定义 oAuth middleware
So first create a middleware in app/Http/Middleware.My middleware name is OauthExceptionMiddleware
在目录app/Http/Middleware
创建一个middleware,名作OauthExceptionMiddleware
分析illuminate\database\Query\Builder.php
- aggregate 方法可执行mysql的方法,如aggregate('group_concact',字段名)
eloquent 查询方法
- first()
- find()
解决eloquen查询时返回数字索引和列名索引问题
"0": 53,
"1": "laravel",
"2": "laravel",
"3": "category",
"4": null,
"5": 1,
"6": 12,
"7": 0,
"8": 0,
"mid": 53,
"name": "laravel",
"slug": "laravel",
"type": "category",
"description": null,
"count": 1,
"order": 12,
"parent": 0,
"addtime": 0
config/database.php
增加一项 'fetch' => PDO::FETCH_CLASS
,像这样:
<?php
return [
/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/
'fetch' => PDO::FETCH_CLASS,
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'blog' => array(
'fetch' => 9,
'driver' => 'mysql',
'host' => env('BLOG_DB_HOST'),
'database' => env('BLOG_DB_NAME'),
'username' => env('BLOG_DB_USERNAME'),
'password' => env('BLOG_DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
],
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => env('DB_PREFIX', ''),
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => env('DB_PREFIX', ''),
'timezone' => env('DB_TIMEZONE', '+00:00'),
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 5432),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => env('DB_PREFIX', ''),
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'prefix' => env('DB_PREFIX', ''),
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'cluster' => env('REDIS_CLUSTER', false),
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
'password' => env('REDIS_PASSWORD', null),
],
],
];
记住fetch在放在最外层,与connections同级
多个order by
OBJ::orderBy('name','desc|asc)
-> orderBy('id','desc|asc')
除/
外其它所有路由都404
nginx
服务器 nginx conf
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
返回的时间(int)转换了datetime
Model类添加方法getCreatedAttribute
,文档 http://laravel-china.org/docs/5.1/eloquent-mutators
public function getCreatedAttribute($value) {
return date('Y-m-d H:i:s',$value);
}
将时区设置为中国上海
编辑.env文件添加
APP_TIMEZONE=PRC
DB_TIMEZONE=+08:00
接收参数page,设置当前页
先执行
$page = $request -> input('page',1);
Paginator::currentPageResolver(function() use ($page) {
return $page;
});
再执行 paginate(每页项数)
关闭model的updated_at
在model或baseModel 添加
public $timestamps = false;
一对一关联模型
mm表 mm
id
name
type_id
类型表 type
id
name
设置model
mm表
function type() {
return $this -> belongsTo('App\Model\Girl\Type','type_id','id');
}
type表暂时不用
- 查询所有mm
$data = Girl\Mm::with(['type'=>function($q) { //$q -> select('*'); }]) -> get();
- 增加时,不允许增id,在model增加
protected $guarded = ['id'];