doulingzou1712 2014-06-06 01:13 采纳率: 0%
浏览 655
已采纳

如何按日期对查询进行排序和分组,并将它们分组到Laravel中的数组中

I want my data grouped by date and sort it. Then I want it to pass it in an array that looks like so:

This is my query:

  1. $events = Event::with('specifications', 'users', 'location')
  2. ->join('locations','location_id', '=', 'events.location_id')
  3. ->where('locations.zip', '=', $data['location'])
  4. ->where('date', '>=', $data['date'])
  5. ->orderBy('date')
  6. ->get();

It gives me a array like this:

  1. [0] => Event Object
  2. (
  3. [...]
  4. [attributes:protected] => Array
  5. (
  6. [id] => 1
  7. [title] => yyy
  8. [image] => img/event/default.jpg
  9. [desc] => Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing eli
  10. )
  11. [....]
  12. )
  13. [1] => Event Object
  14. (
  15. [...]
  16. [attributes:protected] => Array
  17. (
  18. [id] => 2
  19. [title] => xxx
  20. [image] => img/event/default.jpg
  21. [desc] => Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing eli
  22. )
  23. [....]
  24. )

but I want a array that is sortet and grouped like this (you know what i mean ;) ):

  1. [0] = array(
  2. [0] => date = xx.xx.xxxx
  3. [1] => array (
  4. Event Object ()
  5. )
  6. )
  7. [1] = array(
  8. [0] => date = yy.yy.yy
  9. [1] => array (
  10. Event Object ()
  11. )
  12. )

So that i can loop through the array in two loops. one for the date and one for the object. like this bad example:

  1. foreach($event_by_date as $evendate){
  2. <h2>$eventdate</h2>
  3. foreach($eventdate as $event){
  4. $event->name
  5. $event->desc
  6. ... etc ...
  7. }
  8. }

how i do this with laravel and eloquent queries? i'm not a laravel/php ninja ;)

展开全部

  • 写回答

2条回答 默认 最新

  • duansaoguan7955 2014-06-07 05:40
    关注

    my solution. let me know if you would do it smarter:

    1. $data = Input::all();
    2. $data['date'] = date("Y-m-d", strtotime($data['date']));
    3. $events = Event::with('specifications', 'users', 'location')
    4. ->join('locations','location_id', '=', 'events.location_id')
    5. ->where('locations.zip', '=', $data['location'])
    6. ->where('date', '>=', $data['date'])
    7. ->orderBy('date')
    8. ->get();
    9. $events_by_date = array();
    10. $increment = 0;
    11. foreach($events as $event){
    12. $formatted_date = substr($event->date, 0, 10);
    13. if(!isset($temp_date)){
    14. $temp_date = $formatted_date;
    15. } else {
    16. if($temp_date != $formatted_date){
    17. $increment++;
    18. $temp_date = $formatted_date;
    19. }
    20. }
    21. $events_by_date[$increment]['date'] = $formatted_date;
    22. $events_by_date[$increment]['events'][] = $event;
    23. }
    24. return View::make('event.overview')->with('events_by_date', $events_by_date);
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部