Laravel 6 - Get Current URL in a Blade View Example

Laravel 6 , Laravel

Nicesnippets

4383

30-12-2019


Hi Guys

I this tutorial, I will tell you how can you get url, current url with query string and url segment in Laravel. Laravel has a lot of methods to help you with this. They are all methods of Request and can be called statically in Blade without a problem.

Get current url or path in blade using Request method The methods we can use for this are Request::url(), Request::fullUrl(), Request::path(), Request::is() and Request::segment().

here the following example to laravel get current url in a blade view

Get the current path

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

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

Output

Path: post/demo

Get the current url

here using the Request::url() method. It will return 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

here using the Request::fullUrl() method. You 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. 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

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

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

Output

Segment: post

It will help you...


Recommended Posts