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.
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
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">
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,
TheRichPost
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.
Thank you Jasmeen and I am happy that this code helped you..
Useful post Ajay for multiple user types.
Thank you
Thank you ben.
If the User is specified in the HTML Code, couldn’t anyone use Inspect Element to change it to register as an Admin?
yes you are right.