【发布时间】:2014-08-04 04:49:28
【问题描述】:
我正在尝试获得最受欢迎的黑客马拉松,这需要通过相应的黑客马拉松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),因为它根本不起作用。
我将如何尝试根据最多的相关黑客松参与者获得最受欢迎的黑客松?
【问题讨论】:
标签: php mysql laravel eloquent