In this post, I want to explain how to create multi auth laravel. In laravel auth functionality creation is very easy using artisan command. But there are an confusion that how to create multi auth in laravel using artisan command.
Laravel is a very powerful MVC Framework which is works with php artisan command, In laravel we can create fully command based application.
Let’s Get Started
To begin the process of setting up Multi-Authentication in Laravel, we will run the default authentication script. Hopefully you are already familiar with this and what it does. The reason we start from this is that it will give us the baseline to work with. It will set up on of our users right out of the box, and then we will use what it generates to work on our second user type.
1 2 3 | php artisan make:auth |
Now we have a lot of new files generated in our application . The default authentication will be set up with all functionality like login and registration.
Now need to create New Model for another type of user Admin, Copy the below command and run it in terminal
1 2 3 | php artisan make:model Admin |
Now create migration of admin table So please copy the below command and run it in terminal
1 2 3 | php artisan make:migration create_admins_table --create=admins |
This creates a new migration named “create_admins_table” in migration folder database/migrations/
Now we need to edit admin migration file to add whatever content we need to store for admins and generate the table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?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->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('webprepration'); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('admins'); } } |
you can see, I have customized the table for our admins. This is a basic example, but you can add whatever fields you need for admin.
Now need to migrate database, but before use migration command make sure set up database connection in .env file then use below command.
1 2 3 | php artisan migrate |
Now tables are created in database for both types of users, Now need to work with model for both types of users.
Now open Admin models and make changes for user type admin, Please copy the below code and paste in admin model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class Admin extends Authenticatable { use Notifiable; protected $guard = 'admin'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'webprepration', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; } |
In this file some parts are most important like you can see, I have added guard type after notifiable. So we need to add guard in auth.php file. So need to open config/auth.php file and copy the below code and paste in auth.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php return [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'admin-api' => [ 'driver' => 'token', 'provider' => 'admins', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], ] |
As you can see, this time we will set up a guard called “admin” which correlates with the App\Admin.php
model we set up in the previous step where I defined $guard="admin"
.
We can test our functionality right now by creating a new view for admins dashboard. This would represent a page that admin users have access to and customers do not have access to. We will protect it with our new guard so that only admins have access to it.
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.