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

时间:2014-06-06 09:13:56

标签: php mysql arrays laravel eloquent

我希望按日期对数据进行分组并对其进行排序。然后我想让它在一个看起来像这样的数组中传递它:

这是我的疑问:

$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();

它给了我一个这样的数组:

[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
                        )
                     [....]
                  )

但是我想要一个分组的数组并且像这样分组(你知道我的意思;)):

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

这样我就可以在两个循环中循环遍历数组。一个用于日期,一个用于对象。像这个坏榜样:

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

我如何用laravel和雄辩的查询来做到这一点? 我不是laravel / php忍者;)

2 个答案:

答案 0 :(得分:0)

我会手动完成,就像这样;

$sortedArray =
    array_reduce(
         $events,
         function ($result, $element)
         {
              $result[$element->date][] = $element;
              return $result;
         }
    );

无需在此排序,因为您的$events已按日期排序。

此外,您的代码无论如何都无法运行,但是使用我的代码你会去:

foreach ($event_by_date as $date => $events) {
    <h2>$date</h2>
    foreach ($events as $event) {
        $event->name
        $event->desc
        ... etc ...
    }
}

答案 1 :(得分:0)

我的解决方案。让我知道你是否会更聪明地做到这一点:

    $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);