eledana/database/seeders/DatabaseSeeder.php

43 lines
1.1 KiB
PHP
Raw Normal View History

2024-11-07 16:09:43 +00:00
<?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;
2024-11-07 16:09:43 +00:00
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);
2024-11-07 16:09:43 +00:00
User::factory()->create([
2024-11-07 23:56:51 +00:00
'name' => 'superuser',
'email' => 'superuser@admin.com',
'password' => '12341234'
2024-11-07 16:09:43 +00:00
]);
$user = User::find(1); // get a user
$user->assignRole('admin');
2024-11-07 16:09:43 +00:00
}
}