1.1 Create sample data (ok)
https://acquaintsoft.com/blog/laravel-permission-demo
PreviousTích hợp tailwindcss vào scssNext1.2 Hoàn thành 1 project nhỏ Crud dựa trên bài viết gốc (ok)
Last updated
https://acquaintsoft.com/blog/laravel-permission-demo
Last updated
Chú ý:
C:\xampp82\htdocs\lva1\app\Models\User.php
use HasApiTokens, HasFactory, Notifiable, HasRoles;
C:\xampp82\htdocs\lva1\database\seeders\BasicPermissionSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
class BasicPermissionSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// Reset cached roles and permissions
app()[PermissionRegistrar::class]->forgetCachedPermissions();
// create permissions
$permissions = [
'permission list',
'permission create',
'permission edit',
'permission delete',
'role list',
'role create',
'role edit',
'role delete',
'user list',
'user create',
'user edit',
'user delete'
];
foreach ($permissions as $permission) {
Permission::create(['name' => $permission]);
}
// create roles and assign existing permissions
$role1 = Role::create(['name' => 'writer']);
$role1->givePermissionTo('permission list');
$role1->givePermissionTo('role list');
$role1->givePermissionTo('user list');
$role2 = Role::create(['name' => 'admin']);
foreach ($permissions as $permission) {
$role2->givePermissionTo($permission);
}
$role3 = Role::create(['name' => 'super-admin']);
// gets all permissions via Gate::before rule; see AuthServiceProvider
// create demo users
$user = \App\Models\User::factory()->create([
'name' => 'Super Admin',
'email' => 'superadmin@example.com',
]);
$user->assignRole($role3);
$user = \App\Models\User::factory()->create([
'name' => 'Admin User',
'email' => 'admin@example.com',
]);
$user->assignRole($role2);
$user = \App\Models\User::factory()->create([
'name' => 'Example User',
'email' => 'test@example.com',
]);
$user->assignRole($role1);
}
}