Joe*_*aca 45 php mysql laravel eloquent
我正在努力获得最受欢迎的黑客马拉松,需要各自的黑客马拉松订购partipants->count()
.对不起,如果这有点难以理解.
我有一个具有以下格式的数据库:
hackathons
id
name
...
hackathon_user
hackathon_id
user_id
users
id
name
Run Code Online (Sandbox Code Playgroud)
该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');
}
}
Run Code Online (Sandbox Code Playgroud)
并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');
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试Hackathon::orderBy(HackathonParticipant::find($this->id)->count(), 'DESC')->take(5)->get());
但是我觉得我犯了一个大错误(可能是$ this-> id),因为它根本不起作用.
我怎么会去设法得到它是基于相关hackathonParticipants数量最多最流行的这种形式的比赛?
kJa*_*esy 123
这在Laravel 5.3中适用于我,使用您的示例:
Hackathon::withCount('participants')->orderBy('participants_count', 'desc')->paginate(10);
Run Code Online (Sandbox Code Playgroud)
这样就可以在查询中对它进行排序,并且分页效果很好.
use*_*496 34
您应该能够使用Collection
's sortBy()
和count()
方法轻松地完成此操作.
$hackathons = Hackathon::with('participants')->get()->sortBy(function($hackathon)
{
return $hackathon->participants->count();
});
Run Code Online (Sandbox Code Playgroud)
tis*_*chi 19
另一种方法可以是使用withCount()
方法.
Hackathon::withCount('participants')
->orderBy('participants_count', 'desc')
->paginate(50);
Run Code Online (Sandbox Code Playgroud)
参考:https://laravel.com/docs/5.5/eloquent-relationships#querying-relations
小智 13
我有类似的问题,使用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);
Run Code Online (Sandbox Code Playgroud)
小智 6
您还可以使用连接运算符。正如 Sabrina 所说,您不能在数据库级别使用 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);
Run Code Online (Sandbox Code Playgroud)
但是这段代码从数据库中获取所有记录。所以你应该手动分页。
$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();
Run Code Online (Sandbox Code Playgroud)
引用自:https ://stackoverflow.com/a/26384024/2186887
我需要对多个计数求和,然后用它来设置顺序。以下查询在 Laravel 8 中对我有用。
$posts = Post::withCount('comments','likes')->orderBy(\DB::raw('comments_count + likes_count'),'DESC')->get();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30250 次 |
最近记录: |