Create Multiauth Admin and user in Laravel 5+

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.

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

Now create migration of admin table So please copy the below command and run it in terminal

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.

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.

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

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.

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.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *