Categories

Sunday, December 22, 2024
#919814419350 therichposts@gmail.com
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 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

 

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

6 Comments

Leave a Reply

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