49 lines
1.4 KiB
PHP
49 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Department;
|
|
use App\Models\Organization;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class OrganizationSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
$organizations = [];
|
|
|
|
$skip_first = true;
|
|
$org_csv = fopen(base_path('database/data/organizations.csv'), 'r');
|
|
while (($data = fgetcsv($org_csv, 2000, ',')) !== false) {
|
|
if (! $skip_first) {
|
|
$data = array_trim_strings($data);
|
|
$name = $data['0'];
|
|
$organizations[$name] = Organization::create([
|
|
'name' => $name,
|
|
'is_active' => (bool) $data['1'],
|
|
]);
|
|
}
|
|
$skip_first = false;
|
|
}
|
|
fclose($org_csv);
|
|
|
|
$dept_csv = fopen(base_path('database/data/departments.csv'), 'r');
|
|
$skip_first = true;
|
|
while (($data = fgetcsv($dept_csv, 2000, ',')) !== false) {
|
|
if (! $skip_first) {
|
|
$data = array_trim_strings($data, true);
|
|
$name = $data['2'];
|
|
$org_name = $data['1'];
|
|
$org = $organizations[$org_name];
|
|
Department::create([
|
|
'name' => $name,
|
|
'is_active' => (bool) $data['0'],
|
|
'organization_id' => $org->id,
|
|
]);
|
|
}
|
|
$skip_first = false;
|
|
}
|
|
fclose($dept_csv);
|
|
}
|
|
}
|