【发布时间】:2020-04-21 00:03:56
【问题描述】:
这是我的代码:
$search = request()->get('search');
if(Auth::user()->hasRole('admin') || true) {
list($orderBy, $orderDirection) = explode('.', request()->get('sort_by'));
$prestations = Prestation::with(
[
'service' => function($service) use($search){
$service->select(['id','name']);
->where('name', 'regexp', "/$search/i"); <--- HERE THE PROBLEM, i get some "null" values in the output, it's not a really condition to display or not
},
'facility' => function($facility) {
$facility->select(['id','name']);
}
]
)
->where('name', 'regexp', "/$search/i") <-- I want to do this with service name
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
我想对我的“服务:名称”做一个 where 条件,这会真正影响输出,并且不显示值为“null”的数据,例如 ->where('name', 'regexp', "/$search/i"),但我不知道如何访问“with”关系的“service:name”。
谢谢你,新年快乐!
【问题讨论】:
-
您必须为此使用
collection helperwhere,获取不带where子句的记录,然后使用wherehelper 以获得所需的结果。
标签: php laravel eloquent laravel-query-builder