How to get a specific categories with its sub categories item?

Posted 1 month ago by Crazylife

I have this relationship in my model

public function subcategories() {
    return $this->hasMany(self::class, 'id_parent', 'id_category');
}

Let's say i have Fruit as my main parent, sub is frozen and fresh.

When user select fruit, i want to display frozen and fresh item too.

How can i do this in easy way instead of create multiple statement to get all of the id category?

My database structure

id_category | id_parent| name | 
1           |null   | fruit
2           | 1     | frozen
3           | 1     | fresh
4           | 2     | ..

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