Laravel 如果描述相同,则分组选择选项
因此,我有一个选择,选项来自一个laravel ForeachLaravel 如果描述相同,则分组选择选项,laravel,Laravel,因此,我有一个选择,选项来自一个laravel Foreach @foreach($courseMonth as $cm)->groupBy('description'); <optgroup label="{{ nameMonth($cm->month) }}"> @foreach($days as $date) @if(date('m', strtotime($date['date'])) == $cm->mont
@foreach($courseMonth as $cm)->groupBy('description');
<optgroup label="{{ nameMonth($cm->month) }}">
@foreach($days as $date)
@if(date('m', strtotime($date['date'])) == $cm->month)
<option value="{{ date('d/m/Y', strtotime($date['date'])) ." - ". $date['hours'] }}">{{ $date['description'] . ' - ' . date('d/m/Y', strtotime($date['date'])) . ' - ' . $date['hours'] . ' ('. $date['total'] . 'h)'}}</option>
@endif
@endforeach
</optgroup>
<?php $i++;?>
@endforeach
但这没有任何作用。选项的表只有一行(例如:一行只有一个说明,但在选择中可以有多个日期选项),因此我需要仅在两个选项中有相同说明时才对这些选项进行分组
表结构:
-----------------------------------------------
| id | description | date | hours | total |
|---------------------------------------------|
| 4 | work |2019-23-4 | 14h | 6h |
| 5 | work |2019-23-5 | 13h | 5h |
| 6 | school |2019-23-5 | 13h | 5h |
-----------------------------------------------
因此,前两个将出现在同一个选项中,最后一个作为select的第二个选项。请添加带有一些虚拟数据的表结构,如果可能的话。表应该是
@foreach($courseMonth as$cm)->groupBy('description')代码>不是作为$cm的foreach($courseMonth->groupBy('description')的。
?当然,这是假设$courseMonth
是QueryBuilder
,而不是集合。如果是集合
,请展示您的控制器功能以及涉及的每个型号,以组装$courseMonth
。
-----------------------------------------------
| id | description | date | hours | total |
|---------------------------------------------|
| 4 | work |2019-23-4 | 14h | 6h |
| 5 | work |2019-23-5 | 13h | 5h |
| 6 | school |2019-23-5 | 13h | 5h |
-----------------------------------------------