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

Fzoltan87's avatar

Laravel Livewire Product list witdh Categories

Hello. I'm a bit stuck, could you help me? I have created a product list with categories.

How it works: You select a category from the list on the left, and the corresponding products are loaded on the right (the category slug is also added to the URL). If you navigate to a product page, the product's slug is also included in the URL. From the product page, you should be able to go back to the product list.

My problem: If I try to go back to the product list from a product page that belongs to a category with subcategories, I get a 404 error.

If I navigate back from this URL: localhost/products/category_name/sub_category_name/product_name

to this URL: localhost/products/category_name/sub_category_name

The code works fine when trying to go back to the product list from a product page that belongs to a category without subcategories.

If I navigate back from this URL: localhost/products/category_name/product_name

to this URL: localhost/products/category_name

Here is my code:

app\Livewire\ProductPage.php

resources\views\livewire\product-page.blade.php

app\Livewire\ProductDetail.php

resources\views\livewire\product-detail.blade.php

<div class="container mx-auto py-10">
    <div class="max-w-3xl mx-auto bg-white p-6 shadow rounded">
        <h1 class="text-3xl font-bold mb-4">{{ $product->name }}</h1>
        <p class="text-gray-600 mb-4">{{ $product->description }}</p>
        <a href="{{ route('products.category', ['categorySlugPath' => $categorySlugPath]) }}"
            class="text-blue-600 hover:underline mt-4 inline-block">
             ← Back to product list
        </a>
    </div>
</div>

routes\web.php

Route::get('/products', ProductPage::class)->name('products');

Route::get('/products/{categorySlugPath?}/{productSlug}', ProductDetail::class)
    ->where('categorySlugPath', '[a-zA-Z0-9-_/]+')
    ->where('productSlug', '[a-zA-Z0-9-_]+')
    ->name('products.detail');

Route::get('/products/{categorySlugPath?}', ProductPage::class)
    ->where('categorySlugPath', '[a-zA-Z0-9-_/]+')
    ->name('products.category');

resources\views\livewire\partials\category-list.blade.php

0 likes
2 replies
kerelka's avatar

what i can see the problem with is your route between sub categories and product name.

you have conflict routes :

  1. localhost/products/category_name/sub_category_name
  2. localhost/products/category_name/product_name

when you go to route localhost/products/category_name/sub_category_name, it search for product name instead of sub_category_name

because sub_category_name is not a name of your product then you get 404 error.

Fzoltan87's avatar

@kerelka Yes, but theoretically, I have already solved this because there is recursive category traversal in the app\Livewire\ProductPage.php file. At least, I think that if there were an issue with the route, it would also cause problems when navigating to the product details page. However, there is no issue there—only when going back from the product details page.

It is possible that instead of adding the subcategory name to the URL afterward using Livewire, I should construct it in advance and pass it that way.

Please or to participate in this conversation.