Best way to implement nested/hierarchical categories
I am implementing categories into my application, and want to find out what would be the best way to implement nested/hierarchical categories.
By nested/hierarchical I mean that any category can be a child of any other category.
Also, one child category can have multiple parent categories, for example, "Laravel Development" would have both the "Programming" and "Development" as parent categories.
I have considered two options:
Use the Nested Set Model
If I use this model, then the categories model would be a strict hierarchy. The table would contain left and right value fields so that each row could indicate where exactly in the nested set it exists. This means if one child needs to have multiple parents, the child data must be replicated for under each parent. The only advantage I can see for this model in my specific case is that replicated child data under each parent could be amended individually, which might or might not be useful. Could use existing libraries to do this with Laravel.
Use a pivot table (many-to-many)
In this case, I would simply keep the categories table static and use a pivot table (category_category?) to define relations between categories. This means that one category can have as many parents or children as I want without any data duplication. I guess this would be easier to implement in Laravel as I could simply use existing laravel relationships.
So, which option do you think makes more sense and would be easier to extend or maintain in the future?
Why polymorphic? Categories can only have other categories as parents or children. Or do you mean many-to-many relationships, which is what option 2 is in my question?
In any case, I am looking for advice of which option would be better, and why. At the moment I am leaning towards just using many to many relationships using a pivot table.
However I am aware of the Nested Set Model and am wondering why would I use that over the many to many method and vice versa.
@pmall,
Thanks for your reply.
For curiosity sake, would you say there are any advantages to using the Nested Set Model over pivot table based many-to-many hierarchical relationships?
I think you are overthinking this. Use the tools provided by laravel : belongsToMany relationship. It would be a nightmare to manage a nested set system.
I still don't know why so many people suggest nested sets on various websites. Seems like too much work for something that can be done simpler with a pivot table, but then maybe there are some advantages that become more obvious in some very specific cases.
@pmall I know the question is old but would you please show how you implement the migrations? Specifically for something like categories entity with many-to-many or belongsToMany() relationships?
I still don't know why so many people suggest nested sets on various websites. Seems like too much work for something that can be done simpler with a pivot table, but then maybe there are some advantages that become more obvious in some very specific cases.
Nested sets work very well with hierarchical structures, especially when you go more than one level deep, and when you care about sibling order.
I don't understand how you would manage a deep tree structure with pivot tables.
In the specific example of this thread, there is no need for nested sets, because you are not really managing a tree structure. It is better to keep it simple and use normal Eloquent relationships.
When you do need nested sets, there are good packages that can help, such as Baum or Laravel nested set.