如何按日期对查询进行排序和分组,并将它们分组到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 ;)
答
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);
答
I would do it manually, like this;
$sortedArray =
array_reduce(
$events,
function ($result, $element)
{
$result[$element->date][] = $element;
return $result;
}
);
No need to sort here, since your $events
are already sorted by date.
Also, your code wouldn't work anyways, but with my code you'd go:
foreach ($event_by_date as $date => $events) {
<h2>$date</h2>
foreach ($events as $event) {
$event->name
$event->desc
... etc ...
}
}