Posts Learn Components new Snippets new Categories Tags About
/

How to get Current URL from Laravel Blade

Learn how to get the current URL from the Laravel blade template and make use of it to perform your checking logic such as active menu

Created on Oct 06, 2021

33 views

To get the current URL in the Laravel Blade template you can make use of the "url()" helper function which is available globally. This helper can right away be called from the blade template and you can perform conditions based on the value it's returning.

Getting Current URL in Laravel


To get the current URL you can call the "url()" function and then chaining it with the "current()" function which will return the current page URL.
@if (url()->current() === 'your-the-url-value')
    <h1>Do show something here</h1>
@endif
Do note that when using this method you can get the URL path value and from the result, you can determine whether to show or hide values to the user.
@if (Request::is('users'))
   User name 
@endif
Some of the other ways to write the logic above are to write it like below and you will be able to define the active state when you are on the right page.
<ul>
    <li class="{{ Request::is('posts/*') ? 'active' : '' }}">
        Posts
    </li>
</ul>
new

If you like our tutorial, do make sure to support us with some coffee. We need the juice to write the content during the night 🤙🏻