下面由Laravel教程栏目给大家推荐一个laravel极速完成增删改查的第三方包,希望对需要的朋友有所帮助!
推荐一个实用的laravel包https://github.com/osindex/LaravelControllerTrait
可以通过命令行直接生成Model、Controller和migrate文件,并且添加了很多常用的筛选过滤方法,不到一分钟就能写完简单的增删改查
特别是对查询的优化,基本不用单独加接口
laravel-controller-trait
install
composer require osi/laravel-controller-trait
useage
###artisan
PHP artisan trait:controllerPHP artisan trait:model
###controller&&route
use Osi\LaravelControllerTrait\Traits\ControllerBaseTrait; // traituse App\admin; //model fileclass 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/batchrequest()->all(): [ ['ID'=>1,'fIEld'=>'xxx','fIEld2'=>xxx], ['ID'=>2,'fIEld'=>'x2x','fIEld2'=>x2x]]#【1.11】剥离基础返回类use Osi\LaravelControllerTrait\Traits\ResponseBaseTrait; // trait 附带以下方法dataSuccesscreatedacceptednoContentbadRequestunauthorizedforbIDdenunprocesableEtitysuccess
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
【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
Don not code normal controller func.总结
以上是内存溢出为你收集整理的推荐一个laravel极速完成增删改查的第三方包全部内容,希望文章能够帮你解决推荐一个laravel极速完成增删改查的第三方包所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)