From b94494537716738712b119c0f90460e50a2f0cd9 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 15 Mar 2023 14:12:34 -0700 Subject: [PATCH] Remove hard-coded company_id in factories and update seeders --- database/factories/ComponentFactory.php | 1 - database/factories/ConsumableFactory.php | 3 ++- database/factories/UserFactory.php | 3 ++- database/seeders/ComponentSeeder.php | 16 ++++++++++++---- database/seeders/UserSeeder.php | 18 +++++++++++++----- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/database/factories/ComponentFactory.php b/database/factories/ComponentFactory.php index 8dae9f1314..3c186e4442 100644 --- a/database/factories/ComponentFactory.php +++ b/database/factories/ComponentFactory.php @@ -59,7 +59,6 @@ class ComponentFactory extends Factory 'qty' => 10, 'min_amt' => 2, 'location_id' => 3, - 'company_id' => 2, ]; }); } diff --git a/database/factories/ConsumableFactory.php b/database/factories/ConsumableFactory.php index 2875d3d67a..c52ed35367 100644 --- a/database/factories/ConsumableFactory.php +++ b/database/factories/ConsumableFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Models\Category; +use App\Models\Company; use App\Models\Manufacturer; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; @@ -42,6 +43,7 @@ class ConsumableFactory extends Factory 'purchase_cost' => $this->faker->randomFloat(2, 1, 50), 'qty' => $this->faker->numberBetween(5, 10), 'min_amt' => $this->faker->numberBetween($min = 1, $max = 2), + 'company_id' => Company::factory(), ]; } @@ -58,7 +60,6 @@ class ConsumableFactory extends Factory }, 'qty' => 10, 'min_amt' => 2, - 'company_id' => 3, ]; }); } diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index bc23625749..91881d2b2d 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Company; use Illuminate\Database\Eloquent\Factories\Factory; use \Auth; @@ -18,7 +19,7 @@ class UserFactory extends Factory 'activated' => 1, 'address' => $this->faker->address(), 'city' => $this->faker->city(), - 'company_id' => rand(1, 4), + 'company_id' => Company::factory(), 'country' => $this->faker->country(), 'department_id' => rand(1, 6), 'email' => $this->faker->safeEmail, diff --git a/database/seeders/ComponentSeeder.php b/database/seeders/ComponentSeeder.php index 46380e1682..82488879bf 100644 --- a/database/seeders/ComponentSeeder.php +++ b/database/seeders/ComponentSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\Company; use App\Models\Component; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; @@ -12,9 +13,16 @@ class ComponentSeeder extends Seeder { Component::truncate(); DB::table('components_assets')->truncate(); - Component::factory()->count(1)->ramCrucial4()->create(); // 1 - Component::factory()->count(1)->ramCrucial8()->create(); // 1 - Component::factory()->count(1)->ssdCrucial120()->create(); // 1 - Component::factory()->count(1)->ssdCrucial240()->create(); // 1 + + if (! Company::count()) { + $this->call(CompanySeeder::class); + } + + $companyIds = Company::all()->pluck('id'); + + Component::factory()->ramCrucial4()->create(['company_id' => $companyIds->random()]); + Component::factory()->ramCrucial8()->create(['company_id' => $companyIds->random()]); + Component::factory()->ssdCrucial120()->create(['company_id' => $companyIds->random()]); + Component::factory()->ssdCrucial240()->create(['company_id' => $companyIds->random()]); } } diff --git a/database/seeders/UserSeeder.php b/database/seeders/UserSeeder.php index 04f46e29b4..7b6ad44687 100644 --- a/database/seeders/UserSeeder.php +++ b/database/seeders/UserSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\Company; use App\Models\User; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Storage; @@ -17,11 +18,18 @@ class UserSeeder extends Seeder public function run() { User::truncate(); - User::factory()->count(1)->firstAdmin()->create(); - User::factory()->count(1)->snipeAdmin()->create(); - User::factory()->count(3)->superuser()->create(); - User::factory()->count(3)->admin()->create(); - User::factory()->count(50)->viewAssets()->create(); + + if (! Company::count()) { + $this->call(CompanySeeder::class); + } + + $companyIds = Company::all()->pluck('id'); + + User::factory()->count(1)->firstAdmin()->create(['company_id' => $companyIds->random()]); + User::factory()->count(1)->snipeAdmin()->create(['company_id' => $companyIds->random()]); + User::factory()->count(3)->superuser()->create(['company_id' => $companyIds->random()]); + User::factory()->count(3)->admin()->create(['company_id' => $companyIds->random()]); + User::factory()->count(50)->viewAssets()->create(['company_id' => $companyIds->random()]); $src = public_path('/img/demo/avatars/'); $dst = 'avatars'.'/';