推薦一個實用的laravel包github.com/osindex/LaravelControll...
可以通過命令列直接生成Model、Controller和migrate檔案,並且新增了很多常用的篩選過濾方法,不到一分鐘就能寫完簡單的增刪改查
特別是對查詢的優化,基本不用單獨加介面
install
composer require osi/laravel-controller-trait
useage
###artisan
php artisan trait:controller
php artisan trait:model
###controller&&route
use Osi\LaravelControllerTrait\Traits\ControllerBaseTrait; // trait
use App\Admin; //model file
class AdminsController extends Controller
{
use ControllerBaseTrait;
public function __construct(Admin $model)
{
$this->model = $model;
$this->resource = '\Osi\LaravelControllerTrait\Resources\Resource';
$this->collection = '\Osi\LaravelControllerTrait\Resources\Collection';
$this->functions = get_class_methods(self::class);
}
}
Route::resources(['admins' => 'AdminsController']);
#以上完成,即提供了常規的增刪改查方法
#【1.10】新增批量更新
post:api/admins/batch
request()->all(): [
['id'=>1,'field'=>'xxx','field2'=>xxx],
['id'=>2,'field'=>'x2x','field2'=>x2x]
]
#【1.11】剝離基礎返回類
use Osi\LaravelControllerTrait\Traits\ResponseBaseTrait; // trait 附帶以下方法
dataSuccess
created
accepted
noContent
badRequest
unauthorized
forbidden
unprocesableEtity
success
filter
/message?filter={"created_at":{"from":"2016-02-20","to":"2016-02-24 23:59:59"}, "id":{"operation":"not in", "value":[2,3,4]}}
/message?filter={"user_id":{"operation":"in", "value":[null,2,3,4]}}
/message?filter={"id":{"from":2,"to":5}}
/message?filter={"id":{"to":5}} or /message?filter={"id":{"operation":"<=","value":5}}
/message?filter={"updated_at":{"isNull":true}}
/message?filter={"answer":{"operation":"like","value":"Partial search string"}}
/message?filter={"answer":"Full search string"}
/message?filter={"user.name":"asd"} # 關聯搜尋 whereHas
/message?filter={"id":1}
# 暫時只支援單欄位排序
/message?sort=id
/message?sort=-id
/message?sort=user.name
# 關聯搜尋
/message?expand=user
response: { "id": 1, "message": "some message", "user_id": 1, ... "user": { "id": 1, "name": "Some username", ... } }
# 關聯搜尋子集,獲取特定欄位
/message?expand=archives,user.recordable:id/status
# 【1.8】新增scope搜尋
//User Model
<?php
新增允許的filterScopes屬性
protected $filterScopes = ['QueryLike'];
// laravel實現姓名或電話搜尋
public function scopeQueryLike($query, $param)
{
return $query->where(function ($querySec) use ($param) {
return $querySec->where('name', 'like', '%' . $param . '%')->orWhere('phone', 'like', '%' . $param . '%');
});
}
/user?filter={"QueryLike":2333}
# 【1.9】新增JSON搜尋(jsoncontains,jsonlength)
##注:目前僅有jsonlength 支援type屬性
/message?filter={"json->paramA":"233"}
/message?filter={"json->array":{"operation":"jsonlength","type":">","value":5}}
/message?filter={"json->array":{"operation":"jsoncontains","value":5}}
# 【1.11】 filterExpand 用法
## 一般我們使用expand對應with方法 如 `model->with('app')` === `?expand=app`
因此 可以使用 filterExpand 完成 `model->with(['app'=>function($q) use($id){$q->where('id',$id)}])` 的類似方法
/message?expand=app&filterExpand={'app.created_at': { 'operation': '>=', 'value': 'now()' },'app.id': 1}
# 【2.0】 collection 集合增加篩選及分頁方法
#collect()->setFilterAndRelationsAndSort($request)->paginate((int) $request->pageSize ?? 15)
集合的查詢相對資料庫較為簡單 僅包括集合支援的相關方法 具體查閱以下函式
setFilter
#原
post:api/model/batch
request()->all(): [
['id'=>1,'field'=>'xxx','field2'=>xxx],
['id'=>2,'field'=>'x2x','field2'=>x2x]
]
#新增相容 data物件包裹
request()->all(): [
'data'=>
[
['id'=>1,'field'=>'xxx','field2'=>xxx],
['id'=>2,'field'=>'x2x','field2'=>x2x]
]
]
新增"operation":"in" 對null的支援
"col":{"operation":"in", "value":[null,2,3,4]}
func
Don not code normal controller func.
本作品採用《CC 協議》,轉載必須註明作者和本文連結