vincent15000's avatar

Class inheritance and multiple tables

Hello,

I have several roles (admin, collaborator, trainer, student, ...) and each one has some common properties, but also some different properties.

Here are an example of tables I'd like to create.

- users: id, name
- admin_profiles: id, user_id, ...
- trainer_profiles: id, user_id, ...
- student_profiles: id, user_id, ...

What could be fine would be to write a User model and other models Trainer, Student, ... which inherit from the User model.

So when I need to retrieve trainers, I need to query the trainer_profiles table and the users table.

But I really don't see how it is possible with Laravel because the properties are in different tables.

If it's possible to do that, how can I do that ?

Thanks for your help.

Vincent

0 likes
2 replies
tykus's avatar

So you don't know that a given User is a admin, trainer or student without querying each of the _profiles tables for a matching user_id????

1 like
vincent15000's avatar

@tykus Sure I know which role has each user. I have a table like this one.

company_user: company_id, user_id, role

The problem is that an admin can also be a trainer for example. But I should perhaps avoid the users have multiple roles, it would be easier, but this is another reflexion I need to have.

Why I'd like to handle inheritance is because it would be easier to have for example a Trainer model to retrieve all trainers with all informations, for example to display them in a datatable. But also access to specific class methods that are unique to the trainers.

$trainers = Trainer::all();

Rather than.

$trainers = User::with('trainer_profile')->get();

But sure it's quite the same. Perhaps I only want to discover how inheritance between models can be handled with Laravel.

Please or to participate in this conversation.