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

Belhedi's avatar

How to add admin user in my application

Hi every one. I have a conception problem that is I have 3 users in my application (Student, Teacher and Admin). But I want the student and the teacher be in a different models so I have a Teacher model and Student model but the admin isn't a teacher either a student so what I do ? I create a new model for one record isn't best solution or i put it with the teacher or student ? I have no idea !!

0 likes
17 replies
bekaskaki's avatar

create field role or level on your table

example :

`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `sex` char(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `date_birthday` date DEFAULT NULL,
  `phone` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `address` text COLLATE utf8mb4_unicode_ci,
  `role` char(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
Belhedi's avatar

Thnx man for replying.

But exactly create a field role for any table (in other words : the admin should be stored in which table ? the teacher or the student) ?

I guess the field role works perfectly when I have a single Model that called User which collect the whole users of my application and role column identify every user if he is teacher or student or admin

but in my example the teacher and the student have a different models.

tykus's avatar

Do your Teacher and Student models extend a parent User model, or how exactly is authentication handled?

Belhedi's avatar

@TYKUS - well i didn't do the authentication yet cause i didn't know how to add the admin like I said it's a conception problem .

richard's avatar

If only teachers will be logging in, you can use the teachers model for authentication. If students also log in, you need separate multiple authentication guards... but you have to declare their roles separately.

You can use Spatie's Permission library for managing roles.

Belhedi's avatar

@RICHARD - yeah every one should login , the admin , the teacher and also the student. "but you have to declare their roles separately" how ?

richard's avatar

@belhedi Checkout this article. It will give you a hint.

Different users have different roles. So you have to declare what permissions the admin, student, and the teacher has, because I believe they have separate access levels.

Belhedi's avatar

@RICHARD - Thnx man , but i would know when the admin is toredt in the database ? cause i want a code wchich respect my application's conception

richard's avatar
App\Teacher => uses teachers table
App\Student => uses students table

App\User => users table (for Admins only)

On login page, give them a dropdown to choose what kind of user they are. (Login as Student, Teacher or Admin)

Then inside your LoginController, check the option selected and login using a custom guard for that particular user.

tokoiwesley's avatar

@BELHEDI - Typically I would propose the use of only one model for the users and create a roles table for the users (admin, teacher and student).

However, as per your application's conception, I would suggest creating three separate models (Admin, Teacher, Student) with each having a relationship to the User model for easily handling authentication.

1 like
richard's avatar

@tokoiwesley Ah, fellow Nairobian. Mambo kijana

@belhedi Go with Wesley's suggestion to avoid complexity in writing different auth drivers.

create three separate models (Admin, Teacher, Student) with each having a relationship to the User model for easily handling authentication.

1 like
Belhedi's avatar

@TOKOIWESLEY - I'm new to laravel and 2 days ago I have finished the laravel 5.7 from scratch and i want to know how to convert your words into a code ..

tokoiwesley's avatar

You can read more about Eloquent Model relationships here. Below is an example of the four model classes with the respective relationships.

The User model class:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    # Relationships
    public function admin()
    {
        return $this->hasOne(Admin::class, 'user_id', 'id');
    }

    public function teacher()
    {
        return $this->hasOne(Admin::class, 'user_id', 'id');
    }

    public function student()
    {
        return $this->hasOne(Admin::class, 'user_id', 'id');
    }
}

The Admin model class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Admin extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', // table columns with admin details
    ];

    # Inverse of the relationship
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}

The Teacher model class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Teacher extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', // table columns with teacher details
    ];

    # Inverse of the relationship
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}

The Student model class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', // table columns with student details
    ];

    # Inverse of the relationship
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}
1 like
tokoiwesley's avatar

@BELHEDI - Additionally, you can add your admin user in the migrations (as shown below) such that the user records will always be created when you run the migrations.

The Admin can then reset their password (immediately after all migrations have been run) in a production environment and you are good to go.

CreateUsersTable:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

        DB::table('users')->insert([
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => '[email protected]',
            'password' => bcrypt('secret'),
        ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

CreateAdminsTable:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAdminsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id');
            $table->timestamps();
        });

        DB::table('admins')->insert([
            'user_id' => 1,
        ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admins');
    }
}

Please or to participate in this conversation.