database seeder refactor
This commit is contained in:
parent
fa74b70b98
commit
15196c79de
@ -3,11 +3,8 @@
|
|||||||
namespace Database\Seeders;
|
namespace Database\Seeders;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
use Spatie\Permission\Models\Role;
|
use Spatie\Permission\Models\Role;
|
||||||
use Spatie\Permission\Models\Permission;
|
|
||||||
|
|
||||||
class DatabaseSeeder extends Seeder
|
class DatabaseSeeder extends Seeder
|
||||||
{
|
{
|
||||||
@ -16,27 +13,50 @@ class DatabaseSeeder extends Seeder
|
|||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
// User::factory(10)->create();
|
// Create roles if they don't already exist
|
||||||
|
$roles = ['admin', 'privileged'];
|
||||||
// Create roles
|
|
||||||
$adminRole = Role::create(['name' => 'admin']);
|
foreach ($roles as $roleName) {
|
||||||
$privilegedRole = Role::create(['name' => 'privileged']);
|
// Check if the role already exists
|
||||||
|
if (!Role::where('name', $roleName)->exists()) {
|
||||||
// Create permissions
|
Role::create(['name' => $roleName]);
|
||||||
$editPermission = Permission::create(['name' => 'edit articles']);
|
}
|
||||||
$deletePermission = Permission::create(['name' => 'delete articles']);
|
}
|
||||||
|
|
||||||
// Assign permissions to roles
|
// Check if the superuser already exists
|
||||||
$adminRole->givePermissionTo([$editPermission, $deletePermission]);
|
$user = User::firstOrCreate([
|
||||||
$privilegedRole->givePermissionTo($editPermission);
|
|
||||||
|
|
||||||
User::factory()->create([
|
|
||||||
'name' => 'superuser',
|
|
||||||
'email' => 'superuser@admin.com',
|
'email' => 'superuser@admin.com',
|
||||||
'password' => '12341234'
|
'name' => 'superuser',
|
||||||
|
'password' => bcrypt('12341234'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = User::find(1); // get a user
|
// Assign the role to the superuser
|
||||||
$user->assignRole('admin');
|
$user->assignRole('admin');
|
||||||
|
|
||||||
|
// Create other users as before
|
||||||
|
$users = [
|
||||||
|
['name' => 'John Doe', 'email' => 'john@doe.com', 'role' => 'privileged'],
|
||||||
|
['name' => 'Chuck Norris', 'email' => 'chuck@norris.com'],
|
||||||
|
['name' => 'Marios Bros', 'email' => 'mario@bros.com'],
|
||||||
|
['name' => 'Ada Lovelace', 'email' => 'ada@lovelace.com'],
|
||||||
|
['name' => 'Hulk Hogan', 'email' => 'hulk@hogart.com'],
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($users as $userData) {
|
||||||
|
$user = User::firstOrCreate([
|
||||||
|
'email' => $userData['email'],
|
||||||
|
'name' => $userData['name'],
|
||||||
|
'password' => bcrypt('12341234'),
|
||||||
|
]);
|
||||||
|
if (array_key_exists('role', $userData) && $userData['role']) {
|
||||||
|
$user->assignRole($userData['role']);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user