All the relations here must be one-to-zero/one.
A user can be either an employee or a customer. The user_type ENUM gives me the type so I know where to go from there.
Then an employee can be either basic or a manager. The employee_type discriminator let's me know that.
How am I supposed to build the Eloquent Model relations?
Let's say I have a user that is an employee. I need to get it's common fields from the users table but also need to get common fields from employees table. Do I need to hard code, and know that when user_type=emp I need to select from the employees table? What if I need to add another user type later?
@cristian9509 I’d say you have a User model, and then use pivot tables for the relations as to which user managers other users, which users are customers etc.
Why would I use a pivot when I know for sure that a user can only be of one type? So a user that is employee and manager cannot be a customer, cannot be a employee and basic. Am I missing something on my design?
One to One polymorphic relation is suitable for this kind of relationship. Not many to many or not one to many polymorph. It is morphOne relationship .
@veve286 Can you please explain a bit more on how to deal with the creation of a user. Will I have to include lots of IFs to deal with a new user creation?
@bestmomo
I am still trying to figure out why a polymorphic model would be better than a regular one. Let me go on both routes.
Let's go for the following scenario:
I want to access manager_tool data from the managers table for the user with id=11 (the id on the users table).
I want to access customer_status data from the customers table for the user with id=17.
Polymorphic (assuming I have set all the required morph... relations)
I see the difference but don't really understand why would this help me? Is there anything Laravel & Polymorphism specific that I can read and get a better idea?