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

cristian9509's avatar

Polymorphics relations or regular?

I have this schema
Image

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?

0 likes
9 replies
martinbean's avatar

@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.

cristian9509's avatar

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?

bestmomo's avatar

@cristian9509

Can you change your schema or do you want to use Eloquent on this schema without modification ?

cristian9509's avatar

@bestmomo I can still change the schema. However I am also interested to see how or if this is possible with Eloquent!

Would I need to have all my models pointing directly to the user and get rid of the employee table?

User is customer
User is employee_manager
User is employee_basics

veve286's avatar

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 .

cristian9509's avatar

@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?

For example creating a manager employee user:

User->userable->employeable->create(...)

or customer user

User->userable->create(..)
?

bestmomo's avatar

@cristian9509

Looks like a morhOne relation is suitable in this case.

users table should have fields :

  • userable_id
  • userable_type

User model should have a morphTo relation with Employe and Customer

Employe and Customer models should have a morphOne relation with User model.

cristian9509's avatar

@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:

  1. I want to access manager_tool data from the managers table for the user with id=11 (the id on the users table).
  2. 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)

//1
$managerTool = User::find(11)->userable()->employable()->get()->manager_tool;
//2
$customerStatus = User::find(17)->userable()->get()->customer_status;

Normal (assuming again that I have all the required has... belongs... relations)

//1
$managerTool = User::find(11)->employee()->manager()->get()->manager_tool;
//2
$customerStatus = User::find(17)->customer()->get()->customer_status;

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?

2 likes

Please or to participate in this conversation.