Reputation: 331
I am trying to add a "category" header to the dropdown menu as
<ul class="dropdown-menu" aria-labelledby="dropdownMenu3">
...
<li class="dropdown-header">Dropdown header</li>
...
</ul>
The database record is like,
..., Category 1, Item 1
..., Category 1, Item 2
..., Category 2, Item 1
The blade file is as following,
@foreach ($records as $record)
<li role="presentation"><a role="menuitem" tabindex="-1" data-target="#">{{$record->Item}}</a></li>
@endforeach
How can I add $record->Category
into this dropdown so it can be shown as nested as the following,
Category 1
Item 1
Item 2
Category 2
Item 1
...
Upvotes: 0
Views: 455
Reputation: 331
Thanks @mina-youssef , I got it work with something like below:
@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
Upvotes: 3
Reputation: 2981
Pass it through an iterator first to build multi-dimensional array. Something like that:
foreach ($items as $item)
{
$array[$item->category][] = $item->item;
}
Then in your blade go through two @foreach
statements.
Upvotes: 0