From 0f9e39e4cd95c541c020ee088b9c8bf904a4cb83 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Sun, 22 Jan 2017 16:57:09 -0600 Subject: [PATCH] Avoid magic numbers in the seeders, instead adjust order of seeding and load relationships directly. (#3224) --- database/factories/ModelFactory.php | 20 +++++++++++--------- database/factories/UserFactory.php | 4 +--- database/seeds/DatabaseSeeder.php | 4 +++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 1c91b123b7..10afc78161 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -11,26 +11,28 @@ | */ +use App\Models\AssetModel; use App\Models\Category; use App\Models\Company; use App\Models\Location; use App\Models\Manufacturer; +use App\Models\Statuslabel; use App\Models\Supplier; $factory->defineAs(App\Models\Asset::class, 'asset', function (Faker\Generator $faker) { return [ 'name' => $faker->catchPhrase, - 'model_id' => $faker->numberBetween(1, 5), - 'rtd_location_id' => $faker->numberBetween(1, 5), + 'model_id' => AssetModel::inRandomOrder()->first()->id, + 'rtd_location_id' => Location::inRandomOrder()->first()->id, 'serial' => $faker->uuid, - 'status_id' => 1, + 'status_id' => Statuslabel::inRandomOrder()->first()->id, 'user_id' => 1, 'asset_tag' => $faker->unixTime('now'), 'notes' => $faker->sentence, 'purchase_date' => $faker->dateTime(), 'purchase_cost' => $faker->randomFloat(2), 'order_number' => $faker->numberBetween(1000000, 50000000), - 'supplier_id' => $faker->numberBetween(1, 5), + 'supplier_id' => Supplier::inRandomOrder()->first()->id, 'company_id' => Company::inRandomOrder()->first()->id, 'requestable' => $faker->boolean() ]; @@ -40,8 +42,8 @@ $factory->defineAs(App\Models\Asset::class, 'asset', function (Faker\Generator $ $factory->defineAs(App\Models\AssetModel::class, 'assetmodel', function (Faker\Generator $faker) { return [ 'name' => $faker->catchPhrase, - 'manufacturer_id' => $faker->numberBetween(1, 10), - 'category_id' => $faker->numberBetween(1, 9), + 'manufacturer_id' => Manufacturer::inRandomOrder()->first()->id, + 'category_id' => Category::where('category_type', 'asset')->inRandomOrder()->first()->id, 'model_number' => $faker->numberBetween(1000000, 50000000), 'eol' => 1, 'notes' => $faker->paragraph(), @@ -88,7 +90,7 @@ $factory->defineAs(App\Models\Manufacturer::class, 'manufacturer', function (Fak $factory->defineAs(App\Models\Component::class, 'component', function (Faker\Generator $faker) { return [ 'name' => $faker->text(20), - 'category_id' => $faker->numberBetween(21, 25), + 'category_id' => Category::where('category_type', 'component')->inRandomOrder()->first()->id, 'location_id' => Location::inRandomOrder()->first()->id, 'serial' => $faker->uuid, 'qty' => $faker->numberBetween(3, 10), @@ -113,7 +115,7 @@ $factory->defineAs(App\Models\Accessory::class, 'accessory', function (Faker\Gen 'name' => $faker->text(20), 'category_id' => Category::where('category_type', 'accessory')->inRandomOrder()->first()->id, 'manufacturer_id' => Manufacturer::inRandomOrder()->first()->id, - 'location_id' => $faker->numberBetween(1, 5), + 'location_id' => Location::inRandomOrder()->first()->id, 'order_number' => $faker->numberBetween(1000000, 50000000), 'purchase_date' => $faker->dateTime(), 'purchase_cost' => $faker->randomFloat(2), @@ -147,7 +149,7 @@ $factory->defineAs(App\Models\Consumable::class, 'consumable', function (Faker\G return [ 'name' => $faker->text(20), 'company_id' => Company::inRandomOrder()->first()->id, - 'category_id' => $faker->numberBetween(16, 20), + 'category_id' => Category::where('category_type', 'consumable')->inRandomOrder()->first()->id, 'model_number' => $faker->numberBetween(1000000, 50000000), 'item_no' => $faker->numberBetween(1000000, 50000000), 'order_number' => $faker->numberBetween(1000000, 50000000), diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index c0b628e80c..983b64e5b4 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -10,9 +10,7 @@ $factory->defineAs(App\Models\User::class, 'valid-user', function (Faker\Generat 'password' => $faker->password, 'permissions' => '{"user":"0"}', 'email' => $faker->safeEmail, - 'company_id' => function () { - return factory(App\Models\Company::class, 'company')->create()->id; - }, + 'company_id' => Company::inRandomOrder()->first()->id, 'locale' => $faker->locale, 'employee_num' => $faker->numberBetween(3500, 35050), 'jobtitle' => $faker->word, diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 6332a85999..bbbfbcb607 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -22,12 +22,14 @@ class DatabaseSeeder extends Seeder $this->call(LocationSeeder::class); $this->call(SupplierSeeder::class); $this->call(AssetModelSeeder::class); + $this->call(DepreciationSeeder::class); + $this->call(StatuslabelSeeder::class); $this->call(AccessorySeeder::class); $this->call(AssetSeeder::class); $this->call(LicenseSeeder::class); $this->call(ComponentSeeder::class); $this->call(ConsumableSeeder::class); - $this->call(StatuslabelSeeder::class); + $this->call(LicenseSeeder::class); $this->call(ActionlogSeeder::class); $this->call(CustomFieldSeeder::class);