How to display groups of notices by dates one by one using Laravel 5?

夙愿已清 提交于 2019-12-11 13:15:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!