Laravel 5 - 多维GROUPBY,ORDERBY和放大器;分页 [英] Laravel 5 - multi-dimensional groupBy, orderBy & paginate
问题描述
我有一个Post模型,我想 - >分页()
, - > GROUPBY()
和 - >的OrderBy()
。
公共功能指数()
{
$帖=邮政::其中('已验证','1')
- >的OrderBy('created_at'属性,降序)
- > GROUPBY('主题','PUBLISHER_ID)
- >分页(5);
// DD($职位);
}
同时,在数据库中的数据是这样的:
| ID |核实|主题| PUBLISHER_ID |身体| created_at |
| 25 | 1 |森林| 3 | EE |五零年十月十二号|
| 24 | 1 |森林| 3 | DD | 40年11月10日|
| 23 | 1 |森林| 3 | CC |三○年十月十日|
| 22 | 1 |舞蹈| 2 | BB | 9.50.50 |
| 21 | 1 |舞蹈| 2 | AA | 9.40.40 |
| 20 | 1 |音乐| 1 | ZZ | 30年9月30日|
| 19 | 1 |音乐| 1 | XX | 20年9月20号|
| 18 | 1 |艺术| 1 | YY | 9.10.10 |
| 17 | 1 |艺术| 1 | WW | 9.00.00 |
| 16 | 1 |滑雪| 2 | KK | 7.00.00 |
当我取消注释的 DD()
并运行code,我得到这个日志:
LengthAwarePaginator {
...
项目:{
项目:{
0 =>帖子{..}
1 =>帖子{..}
2 =>帖子{..}
3 =>帖子{..}
...
}
}
...
}
0 =>帖子{#249}:published_by:3,体:CC,created_at:三〇年十月十日
1 =>帖子{#250}:published_by:1,体:XX,created_at:20年9月20日
2 =>帖子{#251}:published_by:1,体:WW,created_at:9.00.00
3 =>帖子{#252}:published_by:2,体:KK,created_at:7.00.00
它看起来如此怪异的一个原因。它没有 GROUPBY
用户-3,但不是为别人。此外,这是拉动国内最早创建的,而不是最新的。更改说明
到 ASC
如 - >的OrderBy('created_at'属性,ASC)
将会把一切完全脱离轨道。
在换句话说,返回CC为用户3,森林
而不是'EE'为用户3,森林
然后我想,也许这是 - >分页(5)
搞乱的东西了。
公共职能后()
{
$帖=邮政::在哪里?
...
- >分页(5);
$ postsTry =邮政::其中('已验证','1')
- >的OrderBy('created_at'属性,降序)
- > GROUPBY('主题','PUBLISHER_ID)
- >获得();
// DD($ postsTry);
}
我得到一个集合,只有在项目
像上面它的对象。
( 0 =>邮政{..},1 =>邮政{..},2 =>邮政{...}
)。
它作为分组数据的最早的第一个的而不是最新的第一个的什么我失踪?什么是我做错了的事?
要包裹起来,注意什么我想要得到的是:
'EE'为用户3,森林
BB的用户2,舞蹈
'ZZ'为用户1,音乐
'YY'为用户1,艺术
删除我的previous答案的,但还是会包括的 Laravel分页有关链接 GROUPBY()
与分页()
。
请注意:目前,使用一个
GROUPBY
语句分页操作不能有效地Laravel执行。如果你需要使用GROUPBY
与分页结果集,建议你查询数据库,并手动创建一个分页程序。
更新
由于您的不寻常的日期格式为 created_at
字段,你需要整数铸造
它ED值所使用的为了通过
语句正确(作为一个整数,而不是字符串)。这里有一个工作的例子:
帖子:: selectRaw(*,CAST(REPLACE(created_at,'','')AS无符号整数) CA)
- 化合物其中('已验证','1')
- >的OrderBy(CA,说明)
- > GROUPBY('主题','PUBLISHER_ID)
- >分页();
I have a Post model and I am trying to ->paginate()
, ->groupBy()
and ->orderBy()
.
public function index()
{
$posts = Post::where('verified', '1')
->orderBy('created_at','desc')
->groupBy('topic', 'publisher_id')
->paginate(5);
// dd($posts);
}
Meanwhile, data in Database looks like this:
| id | verified | topic | publisher_id | body | created_at |
| 25 | 1 | Forest | 3 | EE | 10.12.50 |
| 24 | 1 | Forest | 3 | DD | 10.11.40 |
| 23 | 1 | Forest | 3 | CC | 10.10.30 |
| 22 | 1 | Dance | 2 | BB | 9.50.50 |
| 21 | 1 | Dance | 2 | AA | 9.40.40 |
| 20 | 1 | Music | 1 | ZZ | 9.30.30 |
| 19 | 1 | Music | 1 | XX | 9.20.20 |
| 18 | 1 | Art | 1 | YY | 9.10.10 |
| 17 | 1 | Art | 1 | WW | 9.00.00 |
| 16 | 1 | Ski | 2 | KK | 7.00.00 |
When I uncomment the the dd()
and run the code, I get this log:
LengthAwarePaginator {
...
items : {
items : {
0 => Post{..}
1 => Post{..}
2 => Post{..}
3 => Post{..}
...
}
}
...
}
0 => Post{#249} : "published_by: "3", "body": "CC", "created_at": "10.10.30"
1 => Post{#250} : "published_by: "1", "body": "XX", "created_at": "9.20.20"
2 => Post{#251} : "published_by: "1", "body": "WW", "created_at": "9.00.00"
3 => Post{#252} : "published_by: "2", "body": "KK", "created_at": "7.00.00"
It looks so weird for a reason. It did groupBy
for user-3, but not for others. Also, it's pulling the earliest created at rather than latest. Changing desc
to asc
like ->orderBy('created_at', 'asc')
would put everything completely out of track.
In other words, returning 'CC' for user-3, Forest
instead of 'EE' for user-3, Forest
Then I thought maybe it's the ->paginate(5)
messing things up.
public function post()
{
$posts = Post::where...
...
->paginate(5);
$postsTry = Post::where('verified', '1')
->orderBy('created_at','desc')
->groupBy('topic', 'publisher_id')
->get();
// dd($postsTry);
}
I get a Collection with only the items
like above object in it.
(0 => Post{..}, 1 => Post{..}, 2 => Post{..}
).
It's grouping data as being earliest first rather than latest first. What I am missing? What is the thing I am doing wrong?
To wrap up, note that what I want to get is:
'EE' for user-3, Forest
'BB' for user-2, Dance
'ZZ' for user-1, Music
'YY' for user-1, Art
Removed my previous answer but will still include the note from Laravel Pagination about chaining groupBy()
with paginate()
.
Note: 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.
UPDATE
Due to your unusual date format for the created_at
field, you'll need integer cast
ed values for it to used by the order by
statement properly (as an integer, not a string). Here's a working example:
Post::selectRaw("*, CAST(REPLACE(created_at, '.', '') AS UNSIGNED INTEGER) ca")
->where('verified', '1')
->orderBy('ca', 'desc')
->groupBy('topic', 'publisher_id')
->paginate();
这篇关于Laravel 5 - 多维GROUPBY,ORDERBY和放大器;分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!