DEV Community


Posted on

Laravel Multi Auth using Guards with Example (Api)

Image description

  • install laravel global
    composer global require laravel/installer

  • creat project
    laravel new example-app

  • run project
    cd example-app
    php artisan serve

  • Database Configuration

  • Create migration and model for admins
    To make the model and migration for admins table, run the following command:

php artisan make:model Admin -m

It will create migration and model both. From the database/migrations directory, open the admin's migrations file, and edit it as follows:
public function up()
Schema::create('admins', function (Blueprint $table) {

  • migrate database
    php artisan migrate

  • Open the Admin model in app/Models/Admin.php and add the following:
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    class Admin extends Authenticatable
    use Notifiable;
    protected $guard = 'admin';
    protected $fillable = [
    'name', 'email', 'password',
    protected $hidden = [
    'password', 'remember_token',

  • Define the guards , open config/auth.php file
    before adding a new guard, first, we need to add the provider for that because all authentication drivers have a user provider. Let’s add a user provider for admin users where the driver is eloquent and the model is an Admin model class. After adding the provider array will look like below:
    'providers' => [
    'users' => [
    'driver' => 'eloquent',
    'model' => App\Models\User::class,
    'admins' => [
    'driver' => 'eloquent',
    'model' => App\Models\Admin::class,

now we will create a new guard for admin users, where the driver will be session and the provider is admins. after adding that guard array will look like below:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
'admin' => [
'driver' => 'session',
'provider' => 'admins',
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,

  • now got to (app\Providers\RouteServiceProvider.php)
    for make routes admin in file (admin.php)
    write :
    ` */
    public function boot(): void

    $this->routes(function () {
                // Admin User


  • create file route in path (routes\admin.php)
    now write all routes for the

in this file

Top comments (0)