composer require spatie/laravel-permission config/app.php ``` 'providers' => [ // ... Spatie\Permission\PermissionServiceProvider::class, ], ``` php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" php artisan optimize:clear php artisan migrate app/Models/User.php ``` use Illuminate\Foundation\Auth\User as Authenticatable; use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasRoles; //... ``` php artisan make:seeder RolesSeeder database/seeders/RolesSeeder.php ``` use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; //... public function run(): void { $role_admin = Role::create(['name' => 'admin']); $role_normal = Role::create(['name' => 'normal']); $permission_read = Permission::create(['name' => 'read']); $permission_edit = Permission::create(['name' => 'edit']); $permission_write = Permission::create(['name' => 'write']); $permission_delete = Permission::create(['name' => 'delete']); $permissions_admin = [$permission_read, $permission_edit, $permission_write, $permission_delete]; $role_admin->syncPermissions($permissions_admin); $role_normal->givePermissionTo($permission_read); } ``` php artisan make:seeder UserSeeder database/seeders/UserSeeder.php ``` use Illuminate\Support\Facades\Hash; use App\Models\User; public function run(): void { $users = [ [ 'name' => 'Admin', 'email' => 'admin@codingoblin.com', 'password' => 'password', 'role' => 'admin', ], [ 'name' => 'John', 'email' => 'john@codingoblin.com', 'password' => 'password2', 'role' => 'normal', ] ]; foreach($users as $user) { $created_user = User::create([ 'name' => $user['name'], 'email' => $user['email'], 'password' => Hash::make($user['password']), ]); $created_user->assignRole($user['role']); } } ``` database/seeders/DatabaseSeeder.php ``` use Database\Seeders\UserSeeder; use Database\Seeders\RolesSeeder; class DatabaseSeeder extends Seeder { public function run() { $this->call([ RolesSeeder::class, UserSeeder::class, ]); } } ``` php artisan db:seed app/Http/Middleware/HandleInertiaRequests.php ``` public function share(Request $request): array { return array_merge(parent::share($request), [ //... 'user.roles' => $request->user() ? $request->user()->roles->pluck('name') : [], 'user.permissions' => $request->user() ? $request->user()->getPermissionsViaRoles()->pluck('name') : [], ]); } ``` vue ``` {{ $page.props }}
Admin
```