Laravel分页仅按年份和月份分组 [英] Laravel Pagination group by year and month only

查看:320
本文介绍了Laravel分页仅按年份和月份分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从laravel中分页,仅按年份和月份分组
当我使用get时,效果很好

I want to make paginate from laravel, group by year and month only
when i used get , it work well

$data = DB::table('gallery')
        ->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah'),'gallery.*')
        ->where('type','Foto')
        ->groupBy('tanggal')
        ->orderBy('tanggal','desc')
        ->get();

但是当我使用paginate时,出现错误:
SQLSTATE [42S22]:找不到列:1054组语句"中的未知列"tanggal"(SQL:从gallery中选择count(*)作为聚合,其中type =摄影组,由tanggal表示)

but when i use paginate , there is an error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tanggal' in 'group statement' (SQL: select count(*) as aggregate from gallery where type = Foto group by tanggal)

$data = DB::table('gallery')
        ->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah'),'gallery.*')
        ->where('type','Foto')
        ->groupBy('tanggal')
        ->orderBy('tanggal','desc')
        ->paginate(2);

或者对仅按年月度laravel 5.4分组的任何想法?

Or any idea for group by just year and month laravel 5.4?

推荐答案

来自 https://laravel. com/docs/5.4/pagination

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

Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

这是 Jen Segers博客上提供的替代解决方案

这篇关于Laravel分页仅按年份和月份分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆