david19
2 weeks ago

Dont find the right Query

Posted 2 weeks ago by david19

Hello Team, sorry i need help again.

I have a categories table with unlimited subcategories.

ID##Name                ##Parent_ID          ##Category_slug
20##Men                 ##Null                     ##men
21##Women           ##Null                     ##women
22##Jeans               ##20                        ##jeans
23##Leggings        ##21                        ##jeans
24##Blue Jeans          ##22                        ##jeans
25##Red Leggings     ##23                        ##jeans

I made a route for the Shop View, which works fine :)

Route::get('/shop', 'Shop\Category\[email protected]_categories')
public function all_categories()
    {
      $categories = Category::whereNull('parent_id')->get();

      return $categories;
    }
0   
id  20
created_at  "2019-11-28 05:32:53"
updated_at  "2019-11-28 05:32:53"
name    "Men"
parent_id   null
category_slug   "men"
description null
image   null
title   "Men"
alt_img null
canonical_org   null
canonical_new   null
robots  1
meta_description    null
1   
id  21
created_at  "2019-11-28 05:33:03"
updated_at  "2019-11-28 05:33:03"
name    "Women"
parent_id   null
category_slug   "women"
description null
image   null
title   "Women"
alt_img null
canonical_org   null
canonical_new   null
robots  1
meta_description    null

Now i will try get only the subcategories.

Route::get('/shop/{category_slug}', 'Shop\Category\[email protected]_categories')
public function sub_categories(Category $category_slug)
    {

      $categories = Category::whereNotNull('parent_id')->get();

      return $categories;

    }
I visit localhost/shop/men (or women)

But i get all subcategories :(

0   
id  22
created_at  "2019-11-28 05:34:25"
updated_at  "2019-11-28 05:34:25"
name    "Jeans"
parent_id   20
category_slug   "jeans"
description null
image   null
title   "Jeans"
alt_img null
canonical_org   null
canonical_new   null
robots  1
meta_description    null
1   
id  23
created_at  "2019-11-28 05:34:54"
updated_at  "2019-11-28 05:34:54"
name    "Leggings"
parent_id   21
category_slug   "leggings"
description null
image   null
title   "Leggings"
alt_img null
canonical_org   null
canonical_new   null
robots  1
meta_description    null
2   
id  24
created_at  "2019-11-28 05:37:31"
updated_at  "2019-11-28 05:37:31"
name    "Blue Jeans"
parent_id   22
category_slug   "blue-jeans"
description null
image   null
title   "Blue Jeans"
alt_img null
canonical_org   null
canonical_new   null
robots  1
meta_description    null
3   
id  25
created_at  "2019-11-28 05:37:45"
updated_at  "2019-11-28 05:37:45"
name    "Red Leggings"
parent_id   23
category_slug   "red-leggings"
description null
image   null
title   "Red Leggings"
alt_img null
canonical_org   null
canonical_new   null
robots  1
meta_description    null

The View if "men" should only show "Jeans". Sorry for the long text :(

Please sign in or create an account to participate in this conversation.