10 months ago

Users can belong to a user and have many users - How do I design this in the database?

Posted 10 months ago by ryanmortier

I'm building a feature where a user can belong to another user but they can also have many users.

For example a user can be one of four roles: Sales Manager, Regional Manager, Senior Consultant, Sales Representive.

A sales rep can belong to a senior consultant which can belong to a regional manager which can belong to a sales manager. A sales manager can have many regional managers which can have many senior consultants which can have many sales reps.

What would this look like in the database? Would this just be a user_id on the users table?

Secondly, I know I can use the hasManyThrough relationship to skip over the middle relationship, but what if I wanted to, for example, get all sales reps for a sales manager? Can I hasManyThrough a hasManyThrough relationship somehow?


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