获取laravel中最受欢迎的(多对多)记录



我有一个应用程序,用户可以在其中喜欢场地,这是通过与";收藏夹";透视表。

现在,我想检索Eloquent最受欢迎的前10个场馆。

我的型号:

//User model
public function favorites()
{
return $this->belongsToMany('AppModelsVenue', 'favorites', 'user_id', 'venue_id')->withTimeStamps();
}
//Venue model
public function favorites()
{
return $this->belongsToMany('AppModelsUser', 'favorites', 'venue_id', 'user_id')->withTimeStamps();
}

提前感谢!

也许可以试试这个:

$venues = Venue::withCount('favorites')->orderByDesc('favorites_count')->take(10)->get();

最新更新