Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kiasaty's avatar

implementing 3 types of users

Hi guys,

I'm building an application and I'm new to Laravel.

In this application, there are 3 kinds of users that interact with it: admin, donator, charity.

Each one has its own table and model. They can log in to the system.

What is the best way to implement this? They should be in separate tables and should be authorized for doing things after logging in.

any help would be appreciated.

0 likes
6 replies
andreich1980's avatar

Do they really need to be in different tables?

Maybe you can store them all in users with diferent roles. You could even create separate Models for them if you want.

1 like
jlrdw's avatar

The authorization and Authentication with multiple tables versus one table has been covered in the Forum very well before.

Google:

Site:laracasts.com your search term

There is many prior coverage on this.

kiasaty's avatar

@ANDREICH1980 - they have different table fields, and can't be combined in one single table.

I need to consider using one user table with 3 different models. what is the best guide to implement this in a standard way?

jlrdw's avatar

You saying you can't have a table with roles.

You determine if a role matches.

Did you even follow my suggestion above as multiple tables has also been covered.

kiasaty's avatar

@JLRDW - yeah, I checked other threads. But I am looking for the most standard way with minimal change in Laravel behavior.

I can't use one table, because each of these 3 tables has its own schema. honestly, with a little change, It's possible to use the same schema for all three. but this is the last thing I wanna do If there is no standard way to use 3 different tables. I don't wanna fight the framework.

If you wanted to do this, how would you do it to make it work with 3 tables?

grenadecx's avatar

Checkout the answer over at this thread here: https://stackoverflow.com/questions/46292391/authenticate-users-from-more-than-two-tables-in-laravel-5

Baiscally you can add multiple authentication guards and make your 3 different models that are Authenticatable. That way when you login users like:

Auth::guard('admin')->attempt($credentials)
Auth::guard('donator')->attempt($credentials)
Auth::guard('charity')->attempt($credentials)

I would probably not do it this way. Seems a lot easier to deal with one table and roles.

Another way would be to make the users table morhpable. That way you would have like: users, admin, donator, charity tables and users table would have model_id and model_type columns. Those columns are then filled with the corresponding data from either admin, donator or charity table:

// Admin user on User table

model_id = 1
model_type = "App\Admin"

// Charity on User table
model_id = 1
model_type = "App\Charity"

// Donator on User table
model_id = 1
model_type = "App\Donator"

// Then you set the relation, maybe called type so you could

// User model
public function type()
{
    return $this->morphTo();
}

// Example on Admin model

public function user()
{
    return $this->morphOne('App\User', 'model');
}

// Then if you wanted you could add something like isAdmin or isDonator on the User model, all you need to check if the model_type equals to whatever you expect and return true/false. That way you could allow different access to stuffs.

// So you could do
auth()->user()->type // This would then get the data from Admin, Donator or Charity table.

// Check for permissions
auth()->user()->isAdmin();
auth()->user()->isDonator();
auth()->user()->isCharity();

You can read about polymorhpic relations over here: https://laravel.com/docs/5.8/eloquent-relationships#one-to-one-polymorphic-relations

Please or to participate in this conversation.