Laravel在我的搜索系统请求中雄辩地使用orWhere查询

问题描述 投票:0回答:1

我试图用搜索系统编码请求。这里的代码:

$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”获得)。

谢谢。

php laravel search eloquent laravel-query-builder
1个回答
0
投票

尝试此查询。

$prestations = Prestation::with(
                [
                'service' => function($service) use($search){
                    $service->select(['id','name'])->where('name', $serach);
                },
                'facility' => function($facility) {
                    $facility->select(['id','name'])
                }
                ]
            );
© www.soinside.com 2019 - 2024. All rights reserved.