2 回答

TA貢獻1796條經(jīng)驗 獲得超10個贊
請試試這個
$prestations = Prestation::with('service','facility','conciergeries.network')
->whereHas('service', function ($query) use ($searchService) {
$query->select('id','name')->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->select('id','name')->where('name', 'regexp', "/$searchPartenaire/i");
})
->whereHas('conciergeries.network', function ($query) use ($searchFiliale) {
$query->select('id','name')->where('name', 'regexp', "/$searchFiliale/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection);
$res = [
'results' => $prestations->simplePaginate(50),
'total' => $prestations->count(),
];
return $res;

TA貢獻1853條經(jīng)驗 獲得超6個贊
Eloquent 查詢非常靈活,您可以在執(zhí)行它們之前將它們存儲為變量。
這是您可以執(zhí)行的操作的簡化示例:
// Start your query and store it as a variable
$query = Prestation::where('name', 'regexp', "/$search/i");
// Now count the total number of entries which will return
$count = $query->count();
// Finally, do your pagination
$prestations = $query->simplePaginate(50);
當(dāng)然,您可以將所有其他約束添加到查詢中,我剛剛刪除了它們以簡化答案。
分享
編輯
跟隨
于 2020 年 1 月 3 日 1
- 2 回答
- 0 關(guān)注
- 128 瀏覽
添加回答
舉報