我正在尝试将“类别”标题添加到下拉菜单中
<ul class="dropdown-menu" aria-labelledby="dropdownMenu3">
...
<li class="dropdown-header">Dropdown header</li>
...
</ul>
数据库记录就像,
..., Category 1, Item 1
..., Category 1, Item 2
..., Category 2, Item 1
刀片文件如下,
@foreach ($records as $record)
<li role="presentation"><a role="menuitem" tabindex="-1" data-target="#">{{$record->Item}}</a></li>
@endforeach
如何将$record->Category
添加到此下拉列表中,以便它可以显示为嵌套如下,
Category 1
Item 1
Item 2
Category 2
Item 1
...
答案 0 :(得分:2)
谢谢@ mina-youssef,我得到了类似下面的内容:
@foreach ($records->unique('Category') as $record_parrent)
<li class="dropdown-header">{{$record_parrent->Category}}</li>
@foreach ($records->where('Category',$record_parrent->Category) as $record_child)
<li role="presentation"><a role="menuitem" tabindex="-1" data-target="#"> {{$record_child->Item}}</a></li>
@endforeach
@endforeach
答案 1 :(得分:0)
首先将它传递给迭代器以构建多维数组。这样的事情:
foreach ($items as $item)
{
$array[$item->category][] = $item->item;
}
然后在你的刀片中进行两次@foreach
陈述。