下面由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
喜欢就支持一下吧
相关推荐