Sometimes in our application we want to Get Current Url in Blade file Laravel because we want to add specific conditions to show the data on behalf of current url. It can be complete url, route name and query params too so that we can fetch the data accordingly and show to user on basis of conditions in our view or controller.
We can easily get current url in blade laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 application and all example will work in all versions. Majorly we will use url helper and facade function of laravel.
There is multiple ways to find the current url , route name and all information of route with query params in laravel and they are as follow
Get Current URL using url Helper
We will learn to find the query params and current url in this article and also learn to access each of query params by converting them into array so that we can easily parse and use in our view and controller so let’s start the tutorial with methods
Method 1 : Get Current URL using url Helper
To get the url using url helper we need to call as follow
$currentURL = url()->current();
//Or FACADE
$currentURL = /URL::current();
Method 2 : Get Current URL With Query Params
In this example we will get all query params as well with help of full
method of url helper as follow
$urlWithQuery = url()->full();
//Or FACADE
$currentURL = /URL::full();
Method 3 : Get Current URL With Request Class
In this example we will get all query params as well with help of full
method of request class as follow
$urlWithQuery = $request->full();
@if(\Request::url() === 'your url here')
// code
@endif
or we can also get query parameters as follow
request()->get("query_params1");
or to match the path
if (Request::is('admin/*'))
{
// code
}
How to get route name and info in laravel ?
Sometimes we also need to get the route name of current request so we can get as follow
$route = Route::current()->getName();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();