10

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

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

4

5 回答 5

12

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

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

于 2016-01-19T09:57:36.080 回答
2

我知道这是一个老问题,我正在分享我的解决方案以供将来参考。

我设法基于此链接编写了一个函数,该函数完成了确定复杂查询的分页的繁重工作。只需传递“QueryBuilder”,它将返回分页对象/集合。

此外,此过程可以跟踪和维护除 之外的其他参数page=

public function mergeQueryPaginate(\Illuminate\Database\Eloquent\Builder $query): \Illuminate\Pagination\LengthAwarePaginator
    {
        $raw_query = $query;
        $totalCount = $raw_query->get()->count();

        $perPage = request('per-page', 10);
        $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;
    }
于 2019-12-04T10:40:03.640 回答
0
  1. 创建一个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;

  2. 现在为此视图创建一个名为UserSubModel的新模型,protected $table = 'vw_anything';

  3. 现在你的分页查询就像UserSubModel::orderBy('subjects.updated_at', 'desc')->paginate(25);

.

仅按年和月回答这个问题Laravel 分页组

查看查询将是:

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);
于 2019-03-25T09:25:24.120 回答
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'))
于 2016-03-30T06:19:05.603 回答
-1

如果您想分组和分页,这很有效。

$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')
    ->paginate(5);
于 2017-10-26T13:57:08.307 回答