Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SergioGregorutti's avatar

Complex relationships between tables

Hello, I have a project that is a commercial guide, where we can create commerces and assign X number of categories to them. The categories are grouped in up to 3 levels. That is, for example: "Education" -> "Teachers" -> "Spanish"

But, there are cases where there is only one level up or two. Example: "Cars" -> "Driver's Academy"

I know that I have to do "Many to Many" relationships, but how? I need to have a table for each level of categories? How can I assign the relationships on a pivot table?

If anyone can guide me a bit would greatly appreciate it!

I am fairly new to Laravel but hopefully soon I will also collaborate and help others with issues they may have.

Thanks in advance!!

0 likes
2 replies
spescina's avatar
Level 5

Hi! It'a pleasure to see an italian mate around this community. It's quite unusual. You are italian, aren't you?

Anyway, the feature you're trying to implement it's called nested sets. There are a bunch of packages you can install. This, for example, is in active development https://github.com/lazychaser/laravel-nestedset.
Bye

SergioGregorutti's avatar

Awesome!!

Do you know is this works on Laravel 5?

P.S.: Im from Argentina, but my grandparents are from Italy :)

Please or to participate in this conversation.