Can I create a new class that inherits from User in Laravel 5.2?

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 IlluminateFoundationAuthUser 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 IlluminateFoundationAuthUser as Authenticatable;

class Researcher extends Authenticatable
{
//Class definition...
}

...

//Admin Class:
namespace App;

use IlluminateFoundationAuthUser 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.

Thank you so much in advance!

Cheers!

from Newest questions tagged laravel-5 – Stack Overflow http://ift.tt/1S66ryd
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.