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;
|
|
|
|
|
2024-11-07 17:12:11 +00:00
|
|
|
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();
|
|
|
|
|
2024-11-07 17:12:11 +00:00
|
|
|
// 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([
|
|
|
|
'name' => 'Test User',
|
|
|
|
'email' => 'test@example.com',
|
2024-11-07 17:12:11 +00:00
|
|
|
'password' => '12341234'
|
2024-11-07 16:09:43 +00:00
|
|
|
]);
|
2024-11-07 17:12:11 +00:00
|
|
|
|
|
|
|
$user = User::find(1); // get a user
|
|
|
|
$user->assignRole('admin');
|
2024-11-07 16:09:43 +00:00
|
|
|
}
|
|
|
|
}
|