Search code examples
phplaravellaravel-5laravel-6laravel-6.2

Add a class to header when url path changes in Laravel 6.2


How can I add a class when I navigate to the individual blog pages.

For example when a user comes to the Home page the header color should be gold and when the user navigates to each and every single blogs the header background color should be red.

I have done it this way, where I need to add the url path to the header each and every time when I add a new blog page.

<div id="header" class="headertop {{ request()->is('blog','blog/test-one','blog/test-two','blog/test-three') ? 'blog-bg-red' : '' }}" >
<nav></nav>
</div>

Is there a way where i can add the class when the url passes blog/ ?


Solution

  • request->is() uses Str::is() so you could just something like:

    <div id="header" class="headertop {{ request()->is('blog', 'blog/*') ? 'blog-bg-red' : '' }}" >
        <nav></nav>
    </div>
    

    Alternatively, if it's only the index page that's gold and the rest are red, you could do:

    <div id="header" class="headertop {{ request()->is('/') ? '' : 'blog-bg-red' }}" >
        <nav></nav>
    </div>