eledana/database/seeders/DatabaseSeeder.php

43 lines
1.1 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
// Create roles
$adminRole = Role::create(['name' => 'admin']);
$privilegedRole = Role::create(['name' => 'privileged']);
// Create permissions
$editPermission = Permission::create(['name' => 'edit articles']);
$deletePermission = Permission::create(['name' => 'delete articles']);
// Assign permissions to roles
$adminRole->givePermissionTo([$editPermission, $deletePermission]);
$privilegedRole->givePermissionTo($editPermission);
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
'password' => '12341234'
]);
$user = User::find(1); // get a user
$user->assignRole('admin');
}
}