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

cristian9509's avatar

Creating Eloquent Models when having Types and Subtypes (SuperType/Subtype)

I have this database schemathat can be found here Image

I have users that can be either a Business or a Person. My Business users can be of two types: BusinessType1 and BusinessType2. My BusinessType2 has also a few subtypes. Can anyone point me to the right direction into creating the Eloquent Models and how would I be able to access data from a controller?

For example, let's say I need to access special attributes for a Business user that is of Type2 and Subtype2. How do I check, and how do I know which branch I need to look at when having only the Party ID?

0 likes
9 replies
dawiyo's avatar

I would create a table for all types and set a relationship based on that type. Off the top of my head, I would do subtypes as a "series." For example, Type 2, Subtype 2 could be 220 or 22. That way you could do:

$users = User::wherePartyId( $party_id )->whereTypeId( 220 )->get();
cristian9509's avatar

@dawiyo Ok. I get the ideea. But my problem is how to get the 220 number in the first place? Let's say I have in my session only its main ID number stored and from there I need to first know what type and subtype they are. How do I access the specific branch without knowing the "series" number in the first place? Or how do I find out the series number using Eloquent?

dawiyo's avatar

It's kind of hard to help without knowing more specific details. Does this help? (based on if you did a types table):

$user = User::with( 'types' )->find( $some_id );
return $user->types;
cristian9509's avatar

@dawiyo Okay, let me explain a bit. I have a role based access system with different user types getting different roles & permissions. I am still thinking about the DB design since I kinda have redundant data when it comes to user types & roles which in a way are similar. My current issue is how to get the current user's roles and permissions so that I can allow/not allow access to specific modules. Imagine I have a user logged in. Their are type of person. Person type has specific permissions (their role should also be person). That's easy. However, now imagine I have a Business user (Type2 and SubType2) logged in. Based on their ID stored in session at login I want to be able to retrieve their user type & subtypes as well as their role (which is based on type/subtype). Based on that I will know what they can access or not. SubType2 as well as Type2 would hold fields specific to them such as Business_License, License_Expiration_Date, etc. I want to be able to retrieve them using Eloquent functions and because of this I need to know what type/subtype the user is. Person would not hold such data. Example:

  • I have ID=1001 for username=johndoe, password=********, email=johndoe@gmail.com, etc
  • get user_type for ID=1001 => user_type=2 (Business)
  • get business_type from business table => business_type = 2
  • because business_type = 2, get from table business_subtype_2 the business_subtype
  • now that I know the type and subtype I can retrieve all specific fields from the Business branch
dawiyo's avatar

If I comprehending this correctly, I would do it like this: http://www.laravelsd.com/share/azSG2Q

Each user, if they belong to a type, would have an entry in the subsequent types tables which you could then query off of as a relationship.

cristian9509's avatar

@dawiyo But won't I still have to do multiple queries to get the user type? What throws me off is that in my thinking I would have to do multiple queries just to get down to what the user type/subtype is. Isn't there another way of doing this without multiple queries?

dawiyo's avatar

You could store a type_id in the users table.

cristian9509's avatar

That's what I currently have. But then I would have to store a subtype_id in either the user table or the type table. And than I go back to the same issue I initially had!

cristian9509's avatar

Do i need to use some ifs on my users model and create relationships from that ifs? How should i create the models to be able to access the subtypes?

Please or to participate in this conversation.