亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

下面給大家推薦一個(gè)laravel極速完成增刪改查第三方包,希望對(duì)需要的朋友有所幫助!


推薦一個(gè)laravel極速完成增刪改查的第三方包


推薦一個(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.


分享到:
標(biāo)簽:laravel 增刪改查 第三方包
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定