我試圖用搜索系統編寫一個請求。這里的代碼:$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條經驗 獲得超6個贊
試試這個查詢。
$prestations = Prestation::with(
[
'service' => function($service) use($search){
$service->select(['id','name'])->where('name', $search);
},
'facility' => function($facility) {
$facility->select(['id','name'])
}
]
);
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報
0/150
提交
取消