雄辩的-按月,年和分页分组 [英] Eloquent - Group By Month, Year and Paginate

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

问题描述

我正在尝试按月份,年份列出表中的条目:

I am trying to list entries in a table by Month, Year:

  • 2015年5月
    • 第1项
    • 第2项
    • 第3项
    • May, 2015
      • Item 1
      • Item 2
      • Item 3
      • 第1项
      • 等.

      我已经通过以下代码实现了这一目标,但我也想对结果进行分页.我尝试了许多不同的方法,但是似乎都没有用,我正在使用Laravel 5.

      I have achieved this with the following code but I would also like to paginate the results. I have tried many different things but none of them seem to work, I am using Laravel 5.

      $events = App\Events->orderBy('start', 'asc')->get()->groupBy(function($date) {
                  return $date->start->format('F, Y');
              });
      

      以下是上述查询的输出:

      Here is the output for the above query:

      {
          "April, 2015": [
              {
                  "id": "10",
                  "event_type_id": "1",
                  "user_id": "1",
                  "title": "Testing",
                  "slug": "testing",
                  "start": "2015-04-23 17:00:00",
                  "end": "2015-04-23 17:40:00",
                  "description": "<h1>MEETING!</h1><p>Let's try this in HTML!<br></p>",
                  "created_at": "2015-04-19 14:18:33",
                  "updated_at": "2015-04-21 22:07:41",
                  "type": {
                      "id": "1",
                      "name": "General",
                      "slug": "general",
                      "created_at": "2015-04-18 11:24:00",
                      "updated_at": "2015-04-18 11:24:04"
                  }
              }
          ],
          "May, 2015": [
              {
                  "id": "12",
                  "event_type_id": "1",
                  "user_id": "1",
                  "title": "Test Event",
                  "slug": "test-event",
                  "start": "2015-05-15 18:00:00",
                  "end": null,
                  "description": "<p>This is a test event with just a start time</p>",
                  "created_at": "2015-04-21 14:59:56",
                  "updated_at": "2015-05-02 18:37:53",
                  "type": {
                      "id": "1",
                      "name": "General",
                      "slug": "general",
                      "created_at": "2015-04-18 11:24:00",
                      "updated_at": "2015-04-18 11:24:04"
                  }
              },
              {
                  "id": "9",
                  "event_type_id": "1",
                  "user_id": "1",
                  "title": "Monthly Meeting",
                  "slug": "monthly-meeting",
                  "start": "2015-05-23 14:00:00",
                  "end": "2015-04-16 20:00:00",
                  "description": "<p>It's a long monthly meeting!</p>",
                  "created_at": "2015-04-19 13:13:45",
                  "updated_at": "2015-05-03 08:45:56",
                  "type": {
                      "id": "1",
                      "name": "General",
                      "slug": "general",
                      "created_at": "2015-04-18 11:24:00",
                      "updated_at": "2015-04-18 11:24:04"
                  }
              }
          ],
          "June, 2015": [
              {
                  "id": "11",
                  "event_type_id": "1",
                  "user_id": "1",
                  "title": "Another Meeting Saved",
                  "slug": "another-meeting-saved",
                  "start": "2015-06-19 18:00:00",
                  "end": null,
                  "description": "<p>It's another meeting afterall</p>",
                  "created_at": "2015-04-20 15:03:30",
                  "updated_at": "2015-05-03 08:46:19",
                  "type": {
                      "id": "1",
                      "name": "General",
                      "slug": "general",
                      "created_at": "2015-04-18 11:24:00",
                      "updated_at": "2015-04-18 11:24:04"
                  }
              }
          ]
      }
      

      使用LengthAwarePaginator-

      $paginator = new LengthAwarePaginator($events, count($events), 1);
          return $paginator;
      

      这将返回分页器,但数据是相同的-意味着与不使用分页器时相同的结果集,当我希望每页仅返回一条记录时:

      This returns the paginator but the data is the same - meaning the same result set as without the paginator, when I'd expect only one record to be returned per page:

          [{
          "total": 3,
          "per_page": 1,
          "current_page": 1,
          "last_page": 3,
          "next_page_url": "/?page=2",
          "prev_page_url": null,
          "from": 1,
          "to": 3,
          "data": {
              "data" : ".. same as above"
          }
      }]
      

      推荐答案

      对于集合,您需要实现自己的自定义分页器,如文档:

      With aggregates you need to implement your own custom paginator, as stated by docs:

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

      Note: 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.

      请参阅此帖子以手动实现分页:

      See this posts to manually implement pagination:

      Laravel 5-手动分页

      手动创建分页器(Laravel 5)

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

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