I want to have Categories with related Subcategories, It must looks like this:
Category: Broth
Subcategory: Chicken broth, Meat broth, Vegetable broth
And I'll create create_categories_table
& create_subcategories_table
with category_id
I saw some examples with approach that uses: parent_id
and child_id
but I don't understand that logic, maybe it is required for multiple subcategories?
Could you give me some advices for such scenario?
one->many
relationship in yourCategory
&Subcategory
Model class.You will find this in the official docs also laravel.com/docs/12.x/eloquent-relationships#one-to-manycategories
table andCategory
model, which is theparent_id
(no need forchild_id
) scenario you mentioned in your question. The relationship logic in Laravel ends up being more complicated, since it would be a recursive relationship, but there's plenty of tutorials available if you search "Laravel recursive relationship". With your suggested setup, you can only do Categories and Subcategories, anything deeper than that wouldn't work (Broth > Vegetable > Vegan wouldn't be possible for example)