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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料