下面给大家推荐一个laravel极速完成增删改查的第三方包,希望对需要的朋友有所帮助!
推荐一个实用的laravel包:https://github.com/osindex/LaravelControllerTrait
可以通过命令行直接生成Model、Controller和migrate文件,并且添加了很多常用的筛选过滤方法,不到一分钟就能写完简单的增删改查
特别是对查询的优化,基本不用单独加接口
laravel-controller-trait
install
composerrequireosi/laravel-controller-trait
useage
###artisan
phpartisantrait:controllerphpartisantrait:model
###controller&&route
useOsi\LaravelControllerTrait\Traits\ControllerBaseTrait;//traituseApp\Admin;//modelfileclassAdminsControllerextendsController{useControllerBaseTrait;publicfunction__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/batchrequest()->all():[['id'=>1,'field'=>'xxx','field2'=>xxx],['id'=>2,'field'=>'x2x','field2'=>x2x]]#【1.11】剥离基础返回类useOsi\LaravelControllerTrait\Traits\ResponseBaseTrait;//trait附带以下方法dataSuccesscreatedacceptednoContentbadRequestunauthorizedforbiddenunprocesableEtitysuccess
filter
/message?filter={"created_at":{"from":"2016-02-20","to":"2016-02-2423:59:59"},"id":{"operation":"notin","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":"Partialsearchstring"}}/message?filter={"answer":"Fullsearchstring"}/message?filter={"user.name":"asd"}#关联搜索whereHas/message?filter={"id":1}#暂时只支持单字段排序/message?sort=id/message?sort=-id/message?sort=user.name#关联搜索/message?expand=userresponse:{"id":1,"message":"somemessage","user_id":1,…"user":{"id":1,"name":"Someusername",…}}#关联搜索子集,获取特定字段/message?expand=archives,user.recordable:id/status#【1.8】新增scope搜索//UserModel<?php//新增允许的filterScopes属性protected$filterScopes=['QueryLike'];//laravel实现姓名或电话搜索publicfunctionscopeQueryLike($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
【2.1】batch批量更新修改
#原post:api/model/batchrequest()->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
Donnotcodenormalcontrollerfunc.
产品猿社区致力收录更多优质的商业产品,给服务商以及软件采购客户提供更多优质的软件产品,帮助开发者变现来实现多方共赢;
日常运营的过程中我们难免会遇到各种版权纠纷等问题,如果您在社区内发现有您的产品未经您授权而被用户提供下载或使用,您可按照我们投诉流程处理,点我投诉;
本文来自用户发布投稿,不代表产品猿立场 ;若对此文有疑问或内容有严重错误,可联系平台客服反馈;
部分产品是用户投稿,可能本文没有提供官方下下载地址或教程,若您看到的内容没有下载入口,您可以在我们产品园商城搜索看开发者是否有发布商品;若您是开发者,也诚邀您入驻商城平台发布的产品,地址:点我进入;
如若转载,请注明出处:https://www.chanpinyuan.cn/42753.html;