下面由Laravel教程欄目給大家推薦一個(gè)laravel極速完成增刪改查的第三方包,希望對(duì)需要的朋友有所幫助!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、峨邊彝族網(wǎng)站維護(hù)、網(wǎng)站推廣。推薦一個(gè)實(shí)用的laravel包https://github.com/osindex/LaravelControllerTrait
可以通過命令行直接生成Model、Controller和migrate文件,并且添加了很多常用的篩選過濾方法,不到一分鐘就能寫完簡(jiǎn)單的增刪改查
特別是對(duì)查詢的優(yōu)化,基本不用單獨(dú)加接口
laravel-controller-trait
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']); #以上完成,即提供了常規(guī)的增刪改查方法 #【1.10】新增批量更新 post:api/admins/batch request()->all(): [ ['id'=>1,'field'=>'xxx','field2'=>xxx], ['id'=>2,'field'=>'x2x','field2'=>x2x] ] #【1.11】剝離基礎(chǔ)返回類 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"} # 關(guān)聯(lián)搜索 whereHas /message?filter={"id":1} # 暫時(shí)只支持單字段排序 /message?sort=id /message?sort=-id /message?sort=user.name # 關(guān)聯(lián)搜索 /message?expand=user response: { "id": 1, "message": "some message", "user_id": 1, ... "user": { "id": 1, "name": "Some username", ... } } # 關(guān)聯(lián)搜索子集,獲取特定字段 /message?expand=archives,user.recordable:id/status # 【1.8】新增scope搜索 //User Model 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對(duì)應(yīng)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) 集合的查詢相對(duì)數(shù)據(jù)庫較為簡(jiǎn)單 僅包括集合支持的相關(guān)方法 具體查閱以下函數(shù) setFilter
【2.1】batch批量更新修改
#原 post:api/model/batch request()->all(): [ ['id'=>1,'field'=>'xxx','field2'=>xxx], ['id'=>2,'field'=>'x2x','field2'=>x2x] ] #新增兼容 data對(duì)象包裹 request()->all(): [ 'data'=> [ ['id'=>1,'field'=>'xxx','field2'=>xxx], ['id'=>2,'field'=>'x2x','field2'=>x2x] ] ]
添加"operation":"in" 對(duì)null的支持 "col":{"operation":"in", "value":[null,2,3,4]}
func
Don not code normal controller func.