If you need to find out what the current URL is in Blade, 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.
I am going to go through most of the methods that could help you get the current URL or path.
The methods we can use for this are
Get the current path using the
Request::path() method. It will return everything after your domain name. So if the current URL is
http://easylaravel.com/post-title, this method returns
@if (Request::path() == '/post-title') // Do something @endif
Compare the current URL to a pattern using the
Request::is() method. You can even use the
* wildcard. It will return
true if a match is found.
@if (Request::is('admin/*')) // Do something @endif
Get only a segment of the current URL using the method
// Returns true for example.com/admin/post and false for example.com/admin/edit @if (Request::segment(2) == 'post') // Do something @endif
Get the URL without query string using the
Request::url() method. It will return the entire URL, but strip the query string from it.
@if (Request::url() == 'some string') // Do something @endforeach
Get the full URL, including query string using the
Request::fullUrl() method. You probably won't need this method very often, but it can prove to be useful.
@if (str_contains(Request::fullUrl(), 'some-string')) // Do something @endif
Request class can do much more than that, but I will show you more examples in another post, because the other methods aren't directly related to URLs.