Laravel分页不适用于group by子句

问题描述 投票:5回答:4

似乎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

有谁知道我该如何解决这个问题?

laravel laravel-4
4个回答
8
投票

查看文档https://laravel.com/docs/5.2/pagination

目前,使用groupBy语句的分页操作无法由Laravel有效执行。如果需要将groupBy与分页结果集一起使用,建议您查询数据库并手动创建分页器。


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'))

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);

0
投票
  1. 创建一个数据库view namedvw_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;
  2. 现在为此视图创建一个名为UserSubModel的新模型protected $table = 'vw_anything';
  3. 现在您的分页查询将类似于UserSubModel::orderBy('subjects.updated_at', 'desc')->paginate(25);

.

回答这个问题qazxsw poi

查看查询将是:

Laravel Pagination group by year and month only

让你模型是VwModel然后你的分页查询将是

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;
© www.soinside.com 2019 - 2024. All rights reserved.