Part 8 - Adding default role for a user upon registration to the website or app.

Published: 05 April 2023
on channel: Leo Paliuanan
126
4

Topic: LARAVEL PROJECT (E-SHOP) CAPSTONE PROJECT


Part 8 - Adding default role for a user upon registration to the website or app.

this part of our tutorial is very important because we have multiple users for our app

in my case I have the ff roles admin,seller and buyer

so today we will add a default role to whoever register to our website or app. in my case I will make use of the role user as default role to anyone who register to the site..

so lets start

1 first we need to add the default role to the database - seeders - Rolestableseeder and update the database table with php artisan db:seed

currently we dont have a role user
add this line: Role::create(['name' = 'user']);
inside rolestableseeder
run php artisan db:seed




2 - we need to add now the role to the registrationcontroller located inside Controller - auth - registerusercontroller

at the bottom section
look for event(new Registered($user));
add the ff code:

$role = Role::select('id')- where('name','user')- first();
$user- roles()- attach($role);

at the top section of the file call the model of the role.
add the line: use App\Models\Role;




3 - we will add a line that will automatically display the role of the user currently logged in..
goto the resources - views - dashboard.blade.php
add the line:

You are logged in to you {{ __('Dashboard') }} as {{Auth::user()- roles[0]- name}}

it should look like this




x-app-layout
x-slot name="header"
h2 class="font-semibold text-xl text-gray-800 leading-tight"
You are logged in to you {{ __('Dashboard') }} as {{Auth::user()- roles[0]- name}}
/h2
/x-slot



lets test - thats it bye!


Watch video Part 8 - Adding default role for a user upon registration to the website or app. online without registration, duration hours minute second in high quality. This video was added by user Leo Paliuanan 05 April 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 126 once and liked it 4 people.