问题
So if im having a notices
table
where i store title, body and date
.
Now i want to display it in the below format
2018-01-09
Title Body
abc abc123
pqr pqr123
2018-01-10
Title Body
mno mno123
pqr pqr123
So I have input for start date
and end date
which gets all notices between those dates.
$notices = App\Notice::whereBetween('date', [$request->start_date, $request->end_date])->oldest('date')->get();
But now i dont know how to filter data by date and print one group of notices ex. 2018-01-09
first and then another group of notices ex. 2018-01-10
and so on.
How to achieve this ?
回答1:
Use Collection::groupBy method to group data by date field:
$notices = App\Notice::whereBetween('date', [$request->start_date, $request->end_date])
->oldest('date')
->get()
->groupBy('date')
->toArray();
$group1 = $notices['2018-01-09'];
$group2 = $notices['2018-01-10'];
来源:https://stackoverflow.com/questions/48173144/how-to-display-groups-of-notices-by-dates-one-by-one-using-laravel-5