$data = StoneType::with(['user'=>function($query){
// 每个取 2 条
return $query->where(\DB::raw('2'),'>',function($query){
return $query->select(\DB::raw('count(id)'))->from('users as u')
->where('u.stone_id','=','users.stone_id')->where('u.id','>','users.id');
});
}])->limit(4)->orderBy('sort', 'asc')->get();
\DB::getQueryLog
result:
select * from `users` where `users`.`stone_id` in (?, ?, ?, ?) and 2 > (select count(id) from `users` as `u` where `u`.`stone_id` = ? and `u`.`id` > ?)
SQL Result:
eloquent result will get all data like sql select * from users where stone_id in ...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community