Laravel 分页不适用于 group by 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23074147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel pagination not working with group by clause
提问by Anam
It seems Laravel pagination deos not working properly with group by clause. For example:
Laravel 分页 deos 似乎无法与 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);
Produced
出品
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
note that, there is no limit
clause on the query.
请注意,limit
查询中没有子句。
Working fine if no group by clause in the query:
如果查询中没有 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);
produced the following query:
产生了以下查询:
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
does anyone has any idea how can i fix this?
有谁知道我该如何解决这个问题?
回答by Mahendran Kannan
Check the documentation https://laravel.com/docs/5.2/pagination
检查文档 https://laravel.com/docs/5.2/pagination
Currently, pagination operations that use a
groupBy
statement cannot be executed efficiently by Laravel. If you need to use agroupBy
with a paginated result set, it is recommended that you query the database and create a paginator manually.
目前,
groupBy
Laravel 无法有效执行使用语句的分页操作。如果您需要使用groupBy
带有分页结果集的 a,建议您查询数据库并手动创建分页器。
回答by naT erraT
This works for me in laravel 5.2
这在 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'))
回答by user8838201
I think this works if you want to group by and paginate.
如果你想分组和分页,我认为这有效。
$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);
回答by Atiqur
create a database
view
namedvw_anything
. MySql query will be likecreate 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
;Now create a new model named UserSubModelfor this view,
protected $table = 'vw_anything';
Now your paginate query will be like
UserSubModel::orderBy('subjects.updated_at', 'desc')->paginate(25);
创建一个
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
;现在为此视图创建一个名为UserSubModel的新模型,
protected $table = 'vw_anything';
现在你的分页查询会像
UserSubModel::orderBy('subjects.updated_at', 'desc')->paginate(25);
.
.
To answer this questioin Laravel Pagination group by year and month only
仅按年和月回答Laravel 分页组中的此问题
View query will be :
查看查询将是:
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;
Let you model is VwModel then your paginate query will be
让你的模型是 VwModel 那么你的分页查询将是
VwModel::where('type','Foto')->orderBy('tanggal','desc')->paginate(2);
回答by Rafik Farhad
I know it is an old question, by I am sharing my solution for future reference.
我知道这是一个老问题,我正在分享我的解决方案以供将来参考。
I managed to write a function based on this linkwhich does the heavy job of determining the pagination of a complex query. Just pass the 'QueryBuilder' and it will return the paginated object/collection.
我设法根据此链接编写了一个函数,该函数完成了确定复杂查询分页的繁重工作。只需传递“QueryBuilder”,它就会返回分页的对象/集合。
Additionally, this procedure can track and maintain the other parameters except for page=
.
此外,此过程可以跟踪和维护除 之外的其他参数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;
}