Wednesday, June 12, 2024
LaravelLaravl 5.7Php

How to set Laravel Default Auth with custom User type Field?

Set Laravel Default Auth with custom User type Field - The Rich Post

Hello to all, welcome to First of all, I just want to wish you all very Happy Diwali.

In this post, I will tell, How to set Laravel Default Auth with custom User type Field? 

This is very interesting post by my point of view because with this post coding trick, we can do our laravel multiple user type check module. 

I am using hidden input field in this example but I suggest to use select options or checkbox etc.

Here are the following steps, need to follow:


1.  Very first, you need to install fresh laravel project on your pc and here are the steps:

a) $ composer create-project --prefer-dist laravel/laravel laravelmuluser  
//install laravel
b) $ cd laravelmuluser //go inside project folder
c) $ php artisan serve //run project
d) //run project on browser


2. Now, add database name, username, password into you laravel project .env file.



3. Now, run below command to add laravel default authentication into your laravel project:

$ php artisan make:auth

4. Run below commands to check authentication functionality:

$ php artisan serve



5. Now run below command to add laravel authentication tables:

$ php artisan migrate // this will add users table to add user registration form data

6. After run migrate command, you will users table into your laravel project database and add user_type column in it:

ALTER TABLE `users`  ADD `user_type` ENUM('user','admin') NOT NULL DEFAULT 'admin'  AFTER `email`;



7.  Now you need to add below html code  into resources/views/auth/register.blade.php file inside register form:

I have used hidden type just for an example, you can use any field type, which will suitable:

<input type="hidden" name="user_type" value="user">

Set Laravel Default Auth with custom User type Field


8.  Now you need to below code into app/Http/Controllers/Auth/RegisterController.php file to user_type field value into database:

protected function create(array $data)
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'user_type' => $data['user_type'],
            'password' => Hash::make($data['password']),


9.  Now, you need to add below code into app/User.php file to interact to database:

protected $fillable = [
        'name', 'email', 'password', 'user_type'

10. Now you are done with code functionality and now you need to register with new details and you will see:



11. To get user type value from user tables, here is the code, you can use into your laravel project blade template file:

{{ Auth::user()->user_type }}

Now, this done and if you have any query related to this post, then please do comment below or ask question.

Thank you,

Pasand jatt di,



the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.