Laravel 8- Get Current URL in a Blade View Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Laravel 8- Get Current URL in a Blade View Example

Hello Dev’s Today now in this tutorial, I will share with you an example of how can we get url, or current url with query string and also url segment in Laravel application. Now Laravel has a lot of methods to help you with this example. So now they are all methods of Request and also can be called statically in Blade file without a problem.

So get current url or path in blade by using Request method. So the methods we can use for this are Request::url(), or Request::fullUrl(), or Request::path(), Request::is() and also Request::segment().

So here the following examples to laravel by get current url in a blade view:

Get the current path

By using the Request::path() method to get current path.

<p> Path: {{ Request::path() }} </p>

Output

Path: post/demo
Get the current url

Now here by using the Request::url() method. It will return us the entire URL, but strip the query string from it.

<p> Url: {{  Request::url() }} </p>

Output

Url: http://localhost:8000/post/demo
Get the current fullUrl

So here by using the Request::fullUrl() method. we probably won’t need this method very often, but it can prove to be useful.

<p> FullUrl: {{ Request::fullUrl() }} </p>

Output

FullUrl: http://localhost:8000/post/demo
Get current URL to Compare the a pattern

here using the Request::is() method. You can even use the * wildcard. So It will return true if a match is found.

<p> Is: {{ Request::is('post/*') }} </p>

Output

Is: true
Get only a segment of the current URL using the method

Now here by using the Request::segment() method to get current url to segment.

<p> Segment: {{ Request::segment(1) }} </p>

Output

Segment: post

Read Also : Vue JS Get Array Length Or Object Length

I hope it will help you. Also you can follow us on Facebook

About code chef

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 code chef →