Table of contents
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
}
}
}
Â
Now you can validate if the route name exists in your Laravel application. Thanks, Hope it helps :)
Read next