Creating Eloquent Models when having Types and Subtypes (SuperType/Subtype)
I have this database schemathat can be found here
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?
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:
@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 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 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?
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!