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

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


$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.


料青山看我应如是
浏览 149回答 1
1回答

ibeautiful

试试这个查询。$prestations = Prestation::with(                [                'service' => function($service) use($search){                    $service->select(['id','name'])->where('name', $search);                },                'facility' => function($facility) {                    $facility->select(['id','name'])                }                ]            );
打开App,查看更多内容
随时随地看视频慕课网APP