PART 4 - ADDING ROLES AND DEMO USERS ACCOUNT FOR LARAVEL PROJECT

Опубликовано: 06 Март 2023
на канале: Leo Paliuanan
518
7

Topic: LARAVEL PROJECT (E-SHOP) CAPSTONE PROJECT

I will add roles and demo user for my project
I have added a virtual host for my project so I can access it now using the address eshop.test directly from the browser


lets start first by creating a model for the roles table
on terminal run php artisan make:model Role -m
goto database - migrations and look for the role table file
edit code and add the following:

$table- string('name'); // this one is added




lets create another table for the roles assignment table
on terminal run command php artisan make:migration create_role_user_table

this command will not create a model for us coz we already have a model of user, we only need to create the table role_user

open the role_user migration file and add the ff:

$table- integer('role_id'); // added this
$table- integer('user_id');// added this

run php artisan migrate
we are done creating table role and table role_user





next lets create a relationship
goto app\models\ role.php

add the ff:

public function users()
{
return $this- belongsToMany('App\Models\User');
}

do the same process on user model
goto app\models\ user.php at the bottom part add the ff:

public function roles()
{
return $this- belongsToMany('App\Models\Role');
}

we are done creating our relationships for table role and table users










next we need to create a seeder for the demo account for testing purposes
on terminal run php artisan make:seed RolesTableSeeder
open migration file and add the ff:

Role::truncate();
Role::create(['name' = 'admin']);
Role::create(['name' = 'seller']);
Role::create(['name' = 'buyer']);

truncate will delete/remove all records from table role
then create individual roles such as admin seller and buyer


go to databaseseeder.php and make laravel know that we have active seeder by adding these line:

$this- call(RolesTableSeeder::class);
$this- call(UsersTableSeeder::class);

goto terminal and run php artisan make:seed UsersTableSeeder
since we dont have the seeder file for the userstableseeder







open userstableseeder file
add the ff code:

use DB;
use Illuminate\support\facades\Hash;

use App\Models\Role;
use App\Models\User;

on top before the main class

next on the function run()
add ff code:

User::truncate();

DB::table('role_user')- truncate();

$adminRole = Role::Where('name', 'admin')- first();
$sellerRole = Role::Where('name', 'seller')- first();
$buyerRole = Role::Where('name', 'buyer')- first();

$admin = User::create([
'name' = 'Admin User',
'email' = '[email protected]',
'password' = Hash::make('admin***')
]);


$seller = User::create([
'name' = 'Seller',
'email' = '[email protected]',
'password' = Hash::make('seller')
]);

$buyer = User::create([
'name' = 'Buyer',
'email' = '[email protected]',
'password' = Hash::make('buyer')
]);


$admin- roles()- attach($adminRole);
$seller- roles()- attach($sellerRole);
$buyer- roles()- attach($buyerRole);

this will create demo users for us for our app for demo purposes
note: you can change the record with your actual account

run php artisan db:seed to update database table with the roles, roles assignment or role per user and the demo users

check our database for updates


note: roles table contains the lists of roles in my app - like, admin,seller or buyer

role_user is where the assignment of role to a user is being recorded

users table contains the demo account
you can always go back to userstableseeder to check the account access in case you failed to remember..


lets test by logging in with one of our accounts


Смотрите видео PART 4 - ADDING ROLES AND DEMO USERS ACCOUNT FOR LARAVEL PROJECT онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Leo Paliuanan 06 Март 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 518 раз и оно понравилось 7 людям.