TALL Tales

How to Use a Named Route in a Blade View

04 Oct, 2022

Named Routes are really useful to use in your Laravel projects. The main reason for using them is if you think your Route may change and you don't want to have to then go and change every reference to that Route. If you give your Route a name then you won't have to.

Naming a Route

In the file where the Route is kept (usually routes/web.php, but there are others), create the Route you want and then after it add the 'name' method:

// This is the basic Route to the Dashboard page
Route::get('/dashboard', function () {
    return view('dashboard');
});

// This is the same Route with a Name
Route::get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

Using the Named Route in a Blade View

So now we want to reference that Route in the Blade file in one of our anchor tags, for example in the Menu. I've stripped out any extra tags and information and just shown the new <a> tag.

<a href="{{ route('dashboard') }}"> Home </a>

And that's all there is to it.

Photo by specphotops on Unsplash