DEV Community

Cover image for How To Check If Route Name Given Exists in Laravel 8
codeanddeploy
codeanddeploy

Posted on

How To Check If Route Name Given Exists in Laravel 8

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-check-if-route-name-given-exists-in-laravel-8

Do you need to check if the route name given exists in Laravel 8? Sometimes we need to check the route name if exists. For example, if you have a dynamic menu that will call the available routes in your Laravel 8 application then show it as a menu. But if you don't check the route it will throw an error if one of the routes was deleted/removed.

Using Route:has('route_name') provided by Laravel Framework it will help us to check and determine if the route exists.

Okay here is a simple solution:

<?php

namespace App\Http\Controllers;

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

class TestController extends Controller
{
   public function index() 
   {
      if(Route::has('route_name')) {
        //do something here
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

Now you can validate if the route name exists in your Laravel application. I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-check-if-route-name-given-exists-in-laravel-8 if you want to download this code.

Happy coding :)

Discussion (0)