【发布时间】:2019-12-27 22:18:48
【问题描述】:
就像问题的标题一样,我想在 Laravel 中执行查询之前检查表中的一列,例如检查“部门”字段是否为空然后执行查询,如果职位“字段”为空然后执行查询。我想检查表中的列是否为空,而不是数据变量。
DB::table('tasks')
->when('to_departments is not null and to_positions is null', function($query) use ($user){
$query->whereRaw('find_in_set('.$user->department.',to_departments)');
})
->when('to_positions is not null and to_department is null', function($query) use ($user){
$query->whereRaw('find_in_set('.$user->position.',to_positions)');
})
->orderBy('id','desc')
->get();
// with to_departments, to_positions is columns in table
【问题讨论】:
标签: php mysql laravel where-clause laravel-query-builder