DEV Community

Cover image for How To Check If Route Name Given Exists in Laravel 8
Code And Deploy
Code And Deploy

Posted on

2 1

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 :)

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs