我试图用搜索系统编码请求。这里的代码:
$search = request()->get('search');
if(Auth::user()->hasRole('admin') || true)
{
list($orderBy, $orderDirection) = explode('.', request()->get('sort_by'));
$prestations = Prestation::with(
'service:id,name',
'facility:id,name'
)
->orWhere('service:name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
}
问题是,我不知道该如何执行与orWhere
相同的操作->获取与我的$search
相等的所有服务名称(从关系“ with”获得)。
谢谢。
尝试此查询。
$prestations = Prestation::with(
[
'service' => function($service) use($search){
$service->select(['id','name'])->where('name', $serach);
},
'facility' => function($facility) {
$facility->select(['id','name'])
}
]
);