Home / Snippets / How to get Current URL from Laravel Blade
How to get Current URL from Laravel Blade cover

How to get Current URL from Laravel Blade

4.8K

2 years ago

0 comments

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>
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this