I recently took over a project that has an existing user system in place (standard Laravel 5.7 make:auth setup). What I've come to identify is that there is a significant amount of MySQL database columns within the user table that are blank or NULL as a result of the varied user types within the system. For the purpose of this example, let's say they are ADMINISTRATOR, PARENTS, CHILDREN. Within the app currently though, there are no clear distinctions as to what a user is other than a column called "type" which is either NULL for children, 'admin' for ADMINISTRATOR, and 'parent' for PARENTS.
I want to be able to seperate all these columns out to be associated with the appropriate user type while maintining the same authentication method (that is my current thought process, not set in stone).
From what I've gathered, I should be looking at a polymorphic relationship in which the User model has a "typeable" morph relationship to each of the user types. I've also read that that entails having a "typeable_type" and "typeable_id" column on the user model where "type" refers to the model - in this case "App\Models\Admin", "App\Models\Parent", "App\Model\Child".
For each of these I would do something like this:
class User extends Model
{
public function typeable()
{
return $this->morphTo();
}
}
class Admin extends Model
{
public function user()
{
return $this->morphMany('App\User', 'typeable');
}
}
class Parent extends Model
{
public function user()
{
return $this->morphMany('App\User', 'typeable');
}
}
class Child extends Model
{
public function user()
{
return $this->morphMany('App\User', 'typeable');
}
}
My question is - how can I create relationships within the "type" classes that can use the parent User ID as the foreign key / basis of any relationship?
For instance, a Parent might have a subscription in the database that perhaps a Admin doesn't. The subscription table references the current user via a "user_id" column.
Traditionally, the User would just reference the subscription through a normal relationship.
class User extends Model
{
public function subscription()
{
return $this->hasOne(App\Models\Subscription::class);
}
}
class User extends Model
{
public function subscription()
{
return $this->hasMany(App\Models\User::class);
}
}
How would I retrieve that in an architecture where that relationship is owned by the "Parent" class. Take in mind, I'm working within a legacy app that I inherited, so everything currently revolves around the user & user_id.
Long story short, I want to be able to pull different types of info depending on user type but still treat each of the user types as a "User" in the traditional sense. An ideal system would be where the information that each user type possesses is "flattened" into the standard user for use.