[英]Laravel OrderBy relationship count
我正在尝试获得最受欢迎的黑客partipants->count()
,这需要由相应的黑客partipants->count()
的partipants->count()
排序。 对不起,如果这有点难以理解。
我有一个格式如下的数据库:
hackathons
id
name
...
hackathon_user
hackathon_id
user_id
users
id
name
Hackathon
模型是:
class Hackathon extends \Eloquent {
protected $fillable = ['name', 'begins', 'ends', 'description'];
protected $table = 'hackathons';
public function owner()
{
return $this->belongsToMany('User', 'hackathon_owner');
}
public function participants()
{
return $this->belongsToMany('User');
}
public function type()
{
return $this->belongsToMany('Type');
}
}
并且HackathonParticipant
被定义为:
class HackathonParticipant extends \Eloquent {
protected $fillable = ['hackathon_id', 'user_id'];
protected $table = 'hackathon_user';
public function user()
{
return $this->belongsTo('User', 'user_id');
}
public function hackathon()
{
return $this->belongsTo('Hackathon', 'hackathon_id');
}
}
我试过Hackathon::orderBy(HackathonParticipant::find($this->id)->count(), 'DESC')->take(5)->get());
但我觉得我犯了一个大错误(可能是 $this->id),因为它根本不起作用。
我将如何尝试获得基于最多相关黑客马拉松参与者数量的最受欢迎的黑客马拉松?
这在 Laravel 5.3 中对我有用,使用您的示例:
Hackathon::withCount('participants')->orderBy('participants_count', 'desc')->paginate(10);
这样它在查询中排序并且分页效果很好。
编辑:如果使用 Laravel 5.2 或更高版本,请使用 kJamesy 的答案。 它可能会表现得更好一些,因为它不需要将所有参与者和黑客马拉松加载到内存中,只需将分页的黑客马拉松和这些黑客马拉松的参与者数量加载到内存中。
您应该能够使用Collection
的sortBy()
和count()
方法来轻松完成此操作。
$hackathons = Hackathon::with('participants')->get()->sortBy(function($hackathon)
{
return $hackathon->participants->count();
});
另一种方法是使用withCount()
方法。
Hackathon::withCount('participants')
->orderBy('participants_count', 'desc')
->paginate(50);
参考: https : //laravel.com/docs/5.5/eloquent-relationships#querying-relations
我有类似的问题,由于分页,使用 sortBy() 不合适,正如 Sabrina Gelbart 在之前的解决方案中评论的那样。 所以我使用了 db raw,这里是简化的查询:
Tag::select(
array(
'*',
DB::raw('(SELECT count(*) FROM link_tag WHERE tag_id = id) as count_links'))
)->with('links')->orderBy('count_links','desc')->paginate(5);
您还可以使用连接运算符。 正如 Sabrina 所说,您不能在 db 级别使用 sortby。
$hackathons = Hackathon::leftJoin('hackathon_user','hackathon.id','=','hackathon_user.hackathon_id')
->selectRaw('hackathon.*, count(hackathon_user.hackathon_id) AS `count`')
->groupBy('hackathon.id')
->orderBy('count','DESC')
->paginate(5);
但是这段代码从数据库中获取所有记录。 所以你应该手动分页。
$hackathons = Hackathon::leftJoin('hackathon_user','hackathon.id','=','hackathon_user.hackathon_id')
->selectRaw('hackathon.*, count(hackathon_user.hackathon_id) AS `count`')
->groupBy('hackathon.id')
->orderBy('count','DESC')
->skip(0)->take(5)->get();
您可以使用以下代码
Hackathon::withCount('participants')->orderByDesc("participants_count")->paginate(15)
或者,如果您甚至想要使用单一方法的 ASC/DESC
Hackathon::withCount('participants')->orderBy("participants_count", 'asc')->paginate(15)
我需要对多个计数求和,然后用它来设置顺序。 以下查询在 Laravel 8 中对我有用。
$posts = Post::withCount('comments','likes')->orderBy(\DB::raw('comments_count + likes_count'),'DESC')->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.