I am pretty new to laravel (using 5.2 which is the latest version to date), therefore I have the following dilemma: I know that Laravel comes with a User
class right out of the box, but I want to develop a system where I can have another two types of users called Researcher
and Admin
.
My main need to create completely different classes of users (Researcher and Admin), possibly inheriting from User
because the business logic is almost 100% different amongst them and I would not want to create a column in the database to classify the type of user. Furthermore, there aren't many fields that overlap between the stock User
, Admin
and Researcher
classes.
My main question would be: Will everything still work the same (Auth Controller, middleware enabled, etc...) if I inherit from User
for my other 2 classes? I know the principles of OOP and by intuition I assume I should be fine if I do the following:
//'Stock' User class:
//
class User extends Authenticatable{
//Any overlapping logic between Researcher and Admin.
}
class Researcher extends User{
//My class definition here.
}
class Admin extends User{
//My class definition here.
}
And then use both classes as I would normally use an instance of the class User
. By that, I mean use all the methods and right out of the box User
functionality.
QUESTION EXTENSION:
While writing my question, I realize that the class User
looks like so by default:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Could not I just do the following instead of inheriting from User
to create my other 2 classes (Researcher
& Admin
):
//Researcher Class:
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Researcher extends Authenticatable
{
//Class definition...
}
...
//Admin Class:
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
//Class definition
}
In this approach, I am trying to 'mimic' the User
class. The thing is that I don't know if there is any logic in the Kernel that is hardcoded and is being referenced to the User
class implicitly.
Any thought and help will be much appreciated. Sorry if this is silly to you. I am just getting started with the laravel framework.
Cheers!