How We Can Get Route Name In Controller Laravel 8?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares
How We Can Get Route Name In Controller Laravel

Now today In this tutorial, i will show you how we can get our current route name in Laravel 8 application. Here we will be present route name in controller and aslo view file in laravel 8 application. It can be get current route then “Route” facade of currentRouteName function. So we can get current route name laravel 8. So the do the following example. Here we will know How We Can Get Route Name In Controller Laravel 8 application.

I will give a example in blow we can easily get current route name form this example.

Route

Route::get('home', 'HomeController@index')->name('home');

Example:1

If we are want getting route name in controller then “Request” facade will help to get route name.

 /**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index()
{
    $routeName = Request::route()->getName();

    dd($routeName);
} 

Output:

home

Example:2

If we want to get route name in view file or anywhere then we can simply use “Route” facade in currentRouteName function.

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index()
{
   $routeName = Route::currentRouteName(); 

    dd($routeName);
}

Output:

home

Example:3

If we want to get route name in view file or anywhere then we can simply use “Route” facade in current function object

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index()
{
   $routeName = Route::current();
   
    dd($routeName->action['as']);
}

Output:

home

Read Also : How to Disabled Specific dates and Friday,Saturday In Bootstrap Datepicker ?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →