I installed the MenuCRUD from the Backpack Add-On list:
https://github.com/Laravel-Backpack/MenuCRUD#usage-in-your-template
and I put the example code into my blade, but it only shows the first level menu items, as expected.
This is the code I use:
@foreach (\Backpack\MenuCRUD\app\Models\MenuItem::getTree() as $item)
<a class="text-gray-500 hover:text-gray-500" href="{{$item->url()}}">{{ $item->name }}</a>
@endforeach
now the documentation states that I should use $item->children
to access the children of the menu items. I tried it with the following code:
@foreach (\Backpack\MenuCRUD\app\Models\MenuItem::getTree() as $item)
<a class="text-gray-500 hover:text-gray-500" href="{{$item->url()}}">{{ $item->children->name }}</a>
@endforeach
but it gives me this error Property [name] does not exist on this collection instance.
But I can see [NAME]
inside the array list.
[ORIGINAL:PROTECTED] => ARRAY
(
[ID] => 6
[NAME] => Testing-Name
[TYPE] => INTERNAL_LINK
[LINK] => /TEST/LINK1
[PAGE_ID] =>
[PARENT_ID] => 2
[LFT] =>
[RGT] =>
[DEPTH] =>
[CREATED_AT] => 2021-11-06 10:20:20
[UPDATED_AT] => 2021-11-06 10:20:20
[DELETED_AT] =>
)
I called the debugging above over:
@foreach (\Backpack\MenuCRUD\app\Models\MenuItem::getTree() as $item)
{{print_r($item->children)}}
@endforeach
how can I call the children?
CodePudding user response:
if you take a look at the https://github.com/Laravel-Backpack/MenuCRUD/blob/b4a64a454985f8203efa166a2d5dfebc304d2a48/src/app/Models/MenuItem.php#L34
the method you call returns a collection of menu items. Each menu item has an array of children
. This is what actually your debug info shows.
So you should do smth like that:
@foreach (\Backpack\MenuCRUD\app\Models\MenuItem::getTree() as $parent)
@foreach ($parent->children as $child)
@endforeach
@endforeach