溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

laravel實(shí)現(xiàn)極速完成增刪改查的第三方包

發(fā)布時(shí)間:2021-03-12 10:38:34 來(lái)源:億速云 閱讀:220 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下laravel實(shí)現(xiàn)極速完成增刪改查的第三方包,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

推薦一個(gè)實(shí)用的laravel包https://github.com/osindex/LaravelControllerTrait

可以通過(guò)命令行直接生成Model、Controller和migrate文件,并且添加了很多常用的篩選過(guò)濾方法,不到一分鐘就能寫完簡(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
<?php

新增允許的filterScopes屬性
protected $filterScopes = ['QueryLike'];
// laravel實(shí)現(xiàn)姓名或電話搜索
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對(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 集合增加篩選及分頁(yè)方法
#collect()->setFilterAndRelationsAndSort($request)->paginate((int) $request->pageSize ?? 15)
集合的查詢相對(duì)數(shù)據(jù)庫(kù)較為簡(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.

以上是“l(fā)aravel實(shí)現(xiàn)極速完成增刪改查的第三方包”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI