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

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

by therichpost
Published: Last Updated on 6 comments
laravel_auth_with_usertype

Hello to all, welcome to therichpost.com. 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) http://127.0.0.1:8000 //run project on browser

 

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

laravel_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

http://127.0.0.1:8000

laravel_login_register

 

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`;

laravel_database_new_column

 

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:

user_type_value_in_datatabase

 

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,

TheRichPost

 

You may also like

6 comments

Jasmeen November 10, 2018 - 3:35 am

Good tip but I will use user type in select options and thank you for giving solution of my big problem regarding multiple user types.

Reply
Ajay Malhotra November 10, 2018 - 11:51 am

Thank you Jasmeen and I am happy that this code helped you..

Reply
Ben November 10, 2018 - 3:46 am

Useful post Ajay for multiple user types.
Thank you

Reply
Ajay Malhotra November 10, 2018 - 11:50 am

Thank you ben.

Reply
CRUGG August 8, 2020 - 4:42 pm

If the User is specified in the HTML Code, couldn’t anyone use Inspect Element to change it to register as an Admin?

Reply
Ajay Malhotra August 8, 2020 - 4:44 pm

yes you are right.

Reply

Leave a Comment

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