I made a navbar using voyager’s menu builder. I’m trying to add localization functionality to my app but can’t figure out how to pass a {lang}
parameter to this line of code:
@foreach ($items as $menu_item)
<a href='{{ $menu_item->link() }}' class="nav-link">
@endforeach
Normally I would do this:
<a href='{{ route('shopIndex', App::getLocale()) }}' class="nav-link">Shop</a>
I tried doing this which didn’t work:
@foreach ($items as $menu_item)
<a href='{{ route($menu_item->link(), App::getLocale()) }}' class="nav-link">
@endforeach
Any ideas?
2
Answers
SOLVED: running
dd($menu_item)
gave me these attributes:$menu_item->route
gives the route name which I can put in a route helper and pass the parameters I want.Note: some links don't have a route name so you need to define a conditional in case of the links without a route name.
route()
method is used only if you have named routes. Iflink()
only returns a URL, you should not use it with theroute()
method.I see one that you want added by current language, like
http://yoursite/en
. Here are two methods you can follow to solve your problem:$menu_item->link()
to give you the route name and use theroute()
method.