How to display groups of notices by dates one by o

2019-07-25 21:08发布

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条回答
做自己的国王
2楼-- · 2019-07-25 21:49

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'];
查看更多
登录 后发表回答