eledana/database/seeders/DatabaseSeeder.php

90 lines
2.6 KiB
PHP
Raw Normal View History

2024-11-07 16:09:43 +00:00
<?php
namespace Database\Seeders;
use App\Models\User;
use App\Models\Fault;
2024-11-07 16:09:43 +00:00
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
2024-11-07 16:09:43 +00:00
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
2024-11-08 01:28:55 +00:00
// Create roles if they don't already exist
$roles = ['admin', 'privileged'];
foreach ($roles as $roleName) {
// Check if the role already exists
if (!Role::where('name', $roleName)->exists()) {
Role::create(['name' => $roleName]);
}
}
// Check if the superuser already exists
$user = User::firstOrCreate([
2024-11-07 23:56:51 +00:00
'email' => 'superuser@admin.com',
2024-11-08 01:28:55 +00:00
'name' => 'superuser',
'password' => bcrypt('12341234'),
2024-11-07 16:09:43 +00:00
]);
2024-11-08 01:28:55 +00:00
// Assign the role to the superuser
$user->assignRole('admin');
2024-11-08 01:28:55 +00:00
// 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']);
}
}
Fault::firstOrCreate([
'contact_name' => 'Anthony',
'contact_phone' => '666767222',
'description' => 'No tenemos luz en el garaje',
'address' => 'Carrer del mig, 4',
'status' => 'En espera',
'google_maps_link' => 'https://maps.google.com/long/url/link',
'city' => 'Algemesi',
'created_at' => now(),
'updated_at' => now(),
]);
Fault::firstOrCreate([
'contact_name' => 'Amparo',
'contact_phone' => '74645657',
'description' => 'Se ha roto el reloj',
'address' => 'Plaza Mayor 1',
'status' => 'Realizado',
'google_maps_link' => 'https://maps.google.com/long/url/link',
'city' => 'Cataroja',
'created_at' => now(),
'updated_at' => now(),
]);
2024-11-07 16:09:43 +00:00
}
}
2024-11-08 01:28:55 +00:00