Answer the question
In order to leave comments, you need to log in
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
martinfowler.com/eaaCatalog/concreteTableInheritan...
martinfowler.com/eaaCatalog/classTableInheritance.html
There is also Single Table Inheritance, but this is an antipattern.
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 questionAsk a Question
731 491 924 answers to any question