doulingzou1712 2014-06-06 09:13 采纳率: 0%
浏览 652
已采纳

如何按日期对查询进行排序和分组,并将它们分组到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:

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

It gives me a array like this:

[0] => Event Object
            (
                [...]
                [attributes:protected] => Array
                    (
                        [id] => 1
                        [title] => yyy
                        [image] => img/event/default.jpg
                        [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
                    )
                 [....]
              )
[1] => Event Object
                (
                    [...]
                    [attributes:protected] => Array
                        (
                            [id] => 2
                            [title] => xxx
                            [image] => img/event/default.jpg
                            [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
                        )
                     [....]
                  )

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

[0] = array(
             [0] => date = xx.xx.xxxx
             [1] => array (
                          Event Object ()
                          )
           )
[1] = array(
                 [0] => date = yy.yy.yy
                 [1] => array (
                             Event Object ()
                              )
               )

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

foreach($event_by_date as $evendate){
    <h2>$eventdate</h2>
    foreach($eventdate as $event){
        $event->name
        $event->desc
        ... etc ...
    }
}

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

  • 写回答

2条回答 默认 最新

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

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

        $data = Input::all();
    
        $data['date'] = date("Y-m-d", strtotime($data['date']));
    
        $events = Event::with('specifications', 'users', 'location')
                        ->join('locations','location_id', '=', 'events.location_id')
                        ->where('locations.zip', '=', $data['location'])
                        ->where('date', '>=', $data['date'])
                        ->orderBy('date')
                        ->get();
    
        $events_by_date = array();
        $increment = 0;
        foreach($events as $event){
            $formatted_date = substr($event->date, 0, 10);
    
            if(!isset($temp_date)){
                $temp_date = $formatted_date;
            } else {
                if($temp_date != $formatted_date){
                    $increment++;
                    $temp_date = $formatted_date;
                }               
            }
    
            $events_by_date[$increment]['date'] = $formatted_date;
            $events_by_date[$increment]['events'][] = $event;
        }
    
        return View::make('event.overview')->with('events_by_date', $events_by_date);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集