I’ve come across a peculiar bug in a laravel project of mine,
In my web.php I have a named route ‘welcome’ :
Route::get('/', function () { return view('welcome'); })->name('welcome');
This route contain my index page in which there’s a navigation component.
Through an AppServiceProvider I’m passing data to the navigation component
The data im passing is a simple table containing this :
{ name : “home”,
href : “route(‘welcome’)”
}
<nav> @foreach <a href=" {{ myData->href }} "> {{ myData->name }} </a> @endforeach </nav>
The problem is that when I click on the link “home” it sends me to this address :
http://127.0.0.1:8000/route(‘welcome’) https://i.stack.imgur.com/FaET1.png
Instead of http://127.0.0.1:8000/
and this is a problem with every single link, it always returns
http://127.0.0.1:8000/route(‘myLink’)
this is my complete web.php for further information
Route::get('/', [HomeController::class, 'index'])->name('welcome'); Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth', 'verified'])->name('dashboard'); require __DIR__.'/auth.php'; Route::resource('/nav', NavController::class); // // Fallback page error 404 Route::fallback(FallbackController::class);
If you need more information please ask me, i’ve been trying to fix this issue for 2 days, and it is frustrating me.
I tried route:clear, cache:clear, view:clear and nothing changes.
Is it the format at which I’ve stored the name of the route that isn’t working ?
The thing is it was working for an hour or two. and now impossible to fix it.
I hope someone here can help.
Josh’s solution is working.
The route(‘welcome’) was passed through as a string. and the function route() was not called.
Advertisement
Answer
In your data, for the key href
you just need to store welcome
instead of route('welcome')
. Then in your blade template, you can do
<a href="{{ route(myData->href) }}">login here</a>
The reason your solution is not working is that you just have the word route
stored as a string, you aren’t actually calling the function called route
.