S
S
Stanislav Pochepko2015-10-11 14:48:25
Laravel
Stanislav Pochepko, 2015-10-11 14:48:25

How to properly organize model inheritance in laravel?

Good afternoon. How to properly organize the inheritance of models in this case? There is a User model. It is necessary that there are 2 models, Student and Teacher inherited from User. How do I then access the current user through the Auth facade? If I call Auth::user() will I get the base model?
Or maybe make the boolean isTeacher field in the base model, and take the id from the resulting Auth::user() model and look for Teacher::find(Auth::user()->id)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OnYourLips, 2015-10-11
@DJZT

martinfowler.com/eaaCatalog/concreteTableInheritan...
martinfowler.com/eaaCatalog/classTableInheritance.html
There is also Single Table Inheritance, but this is an antipattern.

V
Viktor Pavlov, 2015-11-05
@zogxray

You need roles!
$role = Role::findOrFail($studentRoleId);
$users = $role->users()->paginate(9);
or so
$users = User::with('roles')
->whereHas('roles', function($q){
$q->where('role', '=', 'student');
})- >get();
and share permissions
if(Auth::user()->hasRole('student')) {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question