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

Опубликовано: 05 Апрель 2023
на канале: 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!


Смотрите видео Part 8 - Adding default role for a user upon registration to the website or app. онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Leo Paliuanan 05 Апрель 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 126 раз и оно понравилось 4 людям.