Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lukeboy_2002's avatar

categories menu with colors

Hi,

I want to have a navigation for my categories but when selected the state has te be active. I made a component

my code in my livewire component is:

<x-badge-category wire:navigate
                                          href="{{ route('posts.index', ['category' => $this->activeCategory->slug]) }}"
                                          :Color="$this->activeCategory->color ? 'active' : ''">
                            {{ $this->activeCategory->name }}
                        </x-badge-category>

My badge won't change.

in my PostIndex `php #[Computed] public function posts() { return Post::published() ->with('author', 'comments', 'category') ->search($this->search) ->when($this->activeCategory, function ($query) { $query->withCategory($this->category); }) ->when($this->activeTag, function ($query) { $query->withAnyTags([$this->tag]); }) ->orderBy('published_at', $this->sort) ->paginate(9); }

#[Computed()]
public function activeCategory()
{
    if ($this->category === null || $this->category === '') {
        return null;
    }

    return Category::where('slug', $this->category)->first();
}

#[Computed()]
public function activeTag()
{
    if ($this->tag === null || $this->tag === '') {
        return null;
    }

    return Tag::findFromString($this->tag);
}

How to change the badge to active
0 likes
1 reply
LaryAI's avatar
Level 58

To ensure that your badge changes to the active state correctly, you need to make sure that the active property is being passed correctly to your component. It looks like there might be a small issue with how you're passing the active property in your Livewire component.

Here's a step-by-step solution to address the issue:

  1. Ensure the active property is correctly passed to the component:

    In your Livewire component, you should pass the active property as a boolean to the x-badge-category component. You can do this by checking if the current category is the active category.

  2. Update the Livewire component code:

    Modify your Livewire component to correctly pass the active property. Here's an example of how you can do it:

    <x-badge-category 
        wire:navigate
        href="{{ route('posts.index', ['category' => $this->activeCategory->slug]) }}"
        :color="$this->activeCategory->color"
        :active="$this->activeCategory->slug === $category->slug"
    >
        {{ $this->activeCategory->name }}
    </x-badge-category>
    

    In this example, :active="$this->activeCategory->slug === $category->slug" checks if the current category's slug matches the active category's slug and sets the active property accordingly.

  3. Ensure the active property is used in the component:

    Make sure your Blade component is correctly using the active property to determine the colors. Your component code looks correct, but let's ensure it's complete:

  4. Verify the activeCategory method:

    Ensure that your activeCategory method in the Livewire component is correctly identifying the active category:

    #[Computed]
    public function activeCategory()
    {
        if ($this->category === null || $this->category === '') {
            return null;
        }
    
        return Category::where('slug', $this->category)->first();
    }
    

By following these steps, you should be able to ensure that the badge changes to the active state correctly when a category is selected.

Please or to participate in this conversation.