我试图用搜索系统编写一个请求。这里的代码:$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.
1 回答
ibeautiful
TA贡献1993条经验 获得超5个赞
试试这个查询。
$prestations = Prestation::with(
[
'service' => function($service) use($search){
$service->select(['id','name'])->where('name', $search);
},
'facility' => function($facility) {
$facility->select(['id','name'])
}
]
);
- 1 回答
- 0 关注
- 116 浏览
添加回答
举报
0/150
提交
取消