有一个酒店预订应用程序,我需要获取 4 家预订最多的酒店。
我与Reservation.php中的酒店有关系
public function hotel(){
return $this->belongsTo('App\Hotel');
}
我可以通过以下查询实现我想要的:
Reservation::select(DB::raw('*, COUNT(hotel_id) as hotel_count'))->with('hotel')->groupby('hotel_id')->orderby('hotel_count', 'desc')->limit(4)->get();
是否有任何简化版本可用于上述查询?
更新:数据库结构
最佳答案
Reservation::withCount('hotel')->orderBy('hotel_count', 'desc')->take(4)->get();
看看https://laravel.com/docs/5.6/eloquent-relationships#counting-related-models
关于mysql - Laravel 获得 HitTest 门预订酒店数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49999735/