下面由laravel教程欄目給大家介紹《laravel查詢不在需要寫大量ifelse判斷了,只需要配置一下即可?》,希望對需要的朋友有所幫助!
//若干代碼 根據參數執行不同where if (request('has_score')) { $article = $article->with(['scores' => function ($query) { $query->where('type', self::TYPE); $query->with('user'); }]); } if (has_module('Audit')) { $article = $article->with(['auditing' => function ($query) { $query->orderBy('id', 'desc'); }]); } $article = $article->with(['videos' => function ($query) { $query->where('type', VIDEO); }])->with(['audios' => function ($query) { $ query->where('type', AUDIO); }]);
composer require zyimm/laravelquery-builder
{ "require": { "php": ">=7.0", "fideloper/proxy": "^4.0", "laravel/framework": ">=5.5" } }
/** // 目前支持條件操作符 '=', '<>', '>', '>=', '<', '<=', 'like', 'full_like', 'in', 'not_in', 'between', 'not_between' **/ use IlluminateSupportFacadesDB; use zyimmquerybuildQueryWhere; /** * @var QueryWhere $build */ $build = app('QueryWhere'); //提交過來數據 $data = [ 'log_id' => 20, 'user_id'=> 'zyimm', 'user_name' => "zyimm,12" ]; //配置數據庫字段查詢操作 $condition =[ '=' => [ 'log_id' ], 'not_in' => [ 'user_id' ], 'between' => [ 'user_name' ], 'full_like' => [ 'user_id' ], '<>' => [ 'user_id' ], '>' => [ 'user_id' ] ]; DB::enableQueryLog(); //model AppModelsLog::query() ->where(function ($query) use ($build, $data, $condition){ $build->buildQueryWhere($data ,$condition, $query); })->get(); dd(DB::getQueryLog());
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
喜歡就支持一下吧
相關推薦