sajjad566's avatar

Some routes not working

Hi, I am having an issue with my routes. It is the same route but not working with some of the different data that also exists for example a subcategory page

https://lavender.com.pk/shop/category/sub-category/4/lifestyle

This page is working fine but this same route is not working

https://lavender.com.pk/shop/category/sub-category/28/baking

Although a subcategory with an id of 28 exists as well as its name in baking also.

I am unable to understand programmatically why this is not working. Recently I added a sitemap to the site, seems to have the issue started then. These same names that are not working are also not present in the sitemap. I tried adding them manually but the route still didn't work. The site is live and the links are also working if you want to see them.

0 likes
6 replies
tykus's avatar

Can you share the Route definition?

sajjad566's avatar
Route::get('/shop/category/sub-category/{id}/{name}', [ProductController::class, 'subCategoryShop'])->name('subCategoryShop');
tykus's avatar

So the id and name are wildcards? Are you Route-Model binding; what is the subCategoryShop Controller looking like?

sajjad566's avatar

I haven't made a proper route model binding. Just showing products in a specific subcategory using the id and the name is just a slug.

public function subCategoryShop($id)
    {
        $category = Category::all();
        $cat = Category::findOrFail($id);
        $products = Product::whereHas('subCategory', function($q) use($id){
                         $q->where('sub_category_id', $id); //this refers id field from categories table
                    })
                    ->orderBy('id','desc')
                    ->paginate(18);
        return view('shop.shop', compact(['category', 'products', 'cat']));
    }
tykus's avatar
tykus
Best Answer
Level 104

Are you actually getting to the Controller action (whenever you visit the /shop/category/sub-category/28/baking URI; are you getting a 404 resulting from the findOrFail method?

1 like
sajjad566's avatar

That's it. I mixed the category and subcategory functions a bit. Thanks a lot for pointing that out. I was trying to get a category with a subcategory id which worked in some cases with the same id but not all. Again, thanks alot @tykus. It working now.

Please or to participate in this conversation.