3 回答

TA貢獻2036條經驗 獲得超8個贊
您可以使用子查詢選擇:
business = Business::with('images')
? ? ? ? ->where('isActive', true)
? ? ? ? ? ? ? ? ? ? ->where(function ($q) use ($query){
? ? ? ? ? ? ? ? ? ? ? ? $q->where('name', 'LIKE','%'.$query.'%')->orWhere('description', 'LIKE','%'.$query.'%');
? ? ? ? ? ? ? ? ? ? })
->orderByDesc(['topRating' => Rating::select('rating')
? ? ->whereColumn('business_id', 'businesses.id')
? ? ->orderBy('rating', 'desc')
? ? ->limit(1)])
->get();
請確保設置業務表名稱而不是“業務”

TA貢獻1829條經驗 獲得超7個贊
為此,您必須手動加入表格:
$business = Business::with('images')
->join('rating_table', 'rating_table.business_id', '=', 'business_table.id')
->where('isActive', true)
->where(function ($q) use ($query){
$q->where('name', 'LIKE','%'.$query.'%')
->orWhere('description', 'LIKE','%'.$query.'%');
})
->orderBy('rating_table.value', 'DESC')
->get();
經過多次嘗試,無法使用 Eloquent 按他們的關系對“父”進行排序,您可以在關系內部進行排序,僅此而已。

TA貢獻1826條經驗 獲得超6個贊
我相信你必須像這樣使用連接:
$business = Business::select([
'businesses.*', \DB::raw('AVG(ratings.rating) as avg_rating')
])
->join('ratings', 'businesses.id', '=', 'ratings.business_id')
->orderBy('ratings.avg_rating', 'DESC')
->get();
未測試
- 3 回答
- 0 關注
- 419 瀏覽
添加回答
舉報