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

Laravel Eloquent use the orWhere query in my search system request

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

$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 -> 获取所有服务名称(来自关系 "with")等于我的 $search.

谢谢。

试试这个查询。

$prestations = Prestation::with(
                [
                'service' => function($service) use($search){
                    $service->select(['id','name'])->where('name', $search);
                },
                'facility' => function($facility) {
                    $facility->select(['id','name'])
                }
                ]
            );