Laravel分页似乎与group by子句不能正常工作。例如:
$users = Subject::select(DB::raw('subjects.*, count(user_subjects.id) as total_users')) ->join('user_subjects', 'user_subjects.subject_id', '=', 'subjects.id') ->whereNull('user_subjects.deleted_at') ->groupBy('subjects.id') ->orderBy('subjects.updated_at', 'desc') ->paginate(25);
生产
select subjects.*, count(user_subjects.id) as total_users from `subjects` inner join `user_subjects` on `user_subjects`.`subject_id` = `subjects`.`id` where `subjects`.`deleted_at` is null and `user_subjects`.`deleted_at` is null group by `subjects`.`id` order by `subjects`.`updated_at` desc
请注意,查询中没有limit
子句。
如果查询中没有group by子句,则工作正常:
$users = Subject::select(DB::raw('subjects.*, count(user_subjects.id) as total_users')) ->join('user_subjects', 'user_subjects.subject_id', '=', 'subjects.id') ->whereNull('user_subjects.deleted_at') ->orderBy('subjects.updated_at', 'desc') ->paginate(25);
产生了以下查询:
select subjects.*, count(user_subjects.id) as total_users from `subjects` inner join `user_subjects` on `user_subjects`.`subject_id` = `subjects`.`id` where `subjects`.`deleted_at` is null and `user_subjects`.`deleted_at` is null order by `subjects`.`updated_at` desc limit 25 offset 0
有没有人知道如何解决这个问题?
答案 0 :(得分:8)
查看文档 https://laravel.com/docs/5.2/pagination
目前,使用
groupBy
语句的分页操作不能 由Laravel有效执行。如果您需要使用groupBy
在分页结果集中,建议您查询数据库 并手动创建一个分页器。
答案 1 :(得分:0)
这在laravel 5.2中适用于我
Select(\DB::RAW("assignment_descendant_child.assignment_descendant_child_id, assignment_descendant_child.assignment_descendant_child_name, COUNT(assignment_descendant.assignment_descendant_id) as xNum"))
->leftJoin(
'assignment_descendant',
'assignment_descendant.assignment_descendant_child_id',
'=',
'assignment_descendant_child.assignment_descendant_child_id'
)
->orderBy('assignment_descendant_child_name')
->groupBy('assignment_descendant_child.assignment_descendant_child_id')
->paginate(\Config::get('constants.paginate_org_index'))
答案 2 :(得分:0)
我认为如果你想分组和分页,这是有效的。
$code = DB::table('sources')
->select(DB::raw('sources.id_code,sources.title,avg(point) point'))
->join('rating','sources.id_code','rating.id_code')
->groupBy('sources.id_code')
->groupBy('sources.title')
->groupBy('sources.language')
->groupBy('sources.visited')
->groupBy('sources.')
->paginate(5);
答案 3 :(得分:0)
创建名为view
的数据库vw_anything
。 MySql查询将类似于
create view vw_anything as select subjects.*, count(user_subjects.id) as total_users from subjects inner join user_subjects on user_subjects.subject_id = subjects.id
where
subjects
。deleted_at is null and user_subjects
。deleted_at is null group by
subjects
。id
;
现在为此视图protected $table = 'vw_anything';
现在您的分页查询将类似于UserSubModel::orderBy('subjects.updated_at', 'desc')->paginate(25);
。
要回答此问题Laravel Pagination group by year and month only
查看查询将是:
create view vw_anything as select gallery.*, DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah from gallery group by tanggal;
让您建模为VwModel,那么分页查询将为
VwModel::where('type','Foto')->orderBy('tanggal','desc')->paginate(2);
答案 4 :(得分:0)
我知道这是一个古老的问题,我将分享我的解决方案以供将来参考。
我设法基于this link编写了一个函数,该函数完成了确定复杂查询的分页的繁重工作。只需传递“ QueryBuilder”,它将返回分页的对象/集合。
此外,此过程可以跟踪和维护page=
以外的其他参数。
public function mergeQueryPaginate(\Illuminate\Database\Eloquent\Builder $query): \Illuminate\Pagination\LengthAwarePaginator
{
$raw_query = $query;
$totalCount = $raw_query->get()->count();
$page = request('page', 1);
$skip = $perPage * ($page - 1);
$raw_query = $raw_query->take($perPage)->skip($skip);
$parameters = request()->getQueryString();
$parameters = preg_replace('/&page(=[^&]*)?|^page(=[^&]*)?&?/', '', $parameters);
$path = url(request()->getPathInfo() . '?' . $parameters);
$rows = $raw_query->get();
$paginator = new LengthAwarePaginator($rows, $totalCount, $perPage, $page);
$paginator = $paginator->withPath($path);
return $paginator;
}