How To Check Route Name Exists Or Not In Laravel

Hello Artisan,

In this quick example, I will show you Laravel tips and that is how to check route name exists or not Laravel. I will show you the way to check route name exists or not in both the controller and blade files in Laravel. You know that it is best practice to check something and then echo out them in programming.

So I am here to discuss and explain with an example that Laravel blade check if route exists tutorial, you will learn from an example of how to check route name exists or not in Laravel. So let's see the example code:

First, create a named route like the below example:

routes/web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestController;

Route::get('/', [TestController::class,'create'])->name('article.create');

 

Now we can check this route name in the controller like that:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

class TestController extends Controller
{
    public function create()
    {
        if(Route::has('article.create')) {
           dd(true);
        }

        dd(false);
    }
}

 

And if you want to check it in your blade file, the do like below:

@if(Route::has('article.create'))
     {{ true }}
@else 
     {{ false }}
@endif

 

Read also: What is Signed URL and How and When to Use It in Laravel

 

Hope this how to check route name exists or not Laravel tutorial will help you.

 

#laravel #laravel-8x