Remove hard-coded category_id in factories

This commit is contained in:
Marcus Moore 2023-03-14 12:50:02 -07:00
parent 4c4101fe64
commit a2efe3fa67
No known key found for this signature in database
5 changed files with 69 additions and 39 deletions

View file

@ -2,6 +2,7 @@
namespace Database\Factories;
use App\Models\Category;
use App\Models\Manufacturer;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -46,7 +47,9 @@ class AccessoryFactory extends Factory
return [
'name' => 'Bluetooth Keyboard',
'image' => 'bluetooth.jpg',
'category_id' => 8,
'category_id' => function () {
return Category::where('name', 'Keyboardss')->first() ?? Category::factory()->accessoryKeyboardCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
},
@ -63,7 +66,9 @@ class AccessoryFactory extends Factory
return [
'name' => 'USB Keyboard',
'image' => 'usb-keyboard.jpg',
'category_id' => 8,
'category_id' => function () {
return Category::where('name', 'Keyboardss')->first() ?? Category::factory()->accessoryKeyboardCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
},
@ -80,7 +85,9 @@ class AccessoryFactory extends Factory
return [
'name' => 'Magic Mouse',
'image' => 'magic-mouse.jpg',
'category_id' => 9,
'category_id' => function () {
return Category::where('name', 'Mouse')->first() ?? Category::factory()->accessoryMouseCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
},
@ -97,7 +104,9 @@ class AccessoryFactory extends Factory
return [
'name' => 'Sculpt Comfort Mouse',
'image' => 'comfort-mouse.jpg',
'category_id' => 9,
'category_id' => function () {
return Category::where('name', 'Mouse')->first() ?? Category::factory()->accessoryMouseCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Microsoft')->first() ?? Manufacturer::factory()->microsoft();
},

View file

@ -99,7 +99,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Macbook Pro 13"',
'category_id' => function () {
return Category::find(1) ?? Category::factory()->assetLaptopCategory();
return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory();
},
'eol' => '36',
'depreciation_id' => 1,
@ -115,7 +115,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Macbook Air',
'category_id' => function () {
return Category::find(1) ?? Category::factory()->assetLaptopCategory();
return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
@ -134,7 +134,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Surface',
'category_id' => function () {
return Category::find(1) ?? Category::factory()->assetLaptopCategory();
return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Microsoft')->first() ?? Manufacturer::factory()->microsoft();
@ -153,7 +153,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'XPS 13',
'category_id' => function () {
return Category::find(1) ?? Category::factory()->assetLaptopCategory();
return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Dell')->first() ?? Manufacturer::factory()->dell();
@ -172,7 +172,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'ZenBook UX310',
'category_id' => function () {
return Category::find(1) ?? Category::factory()->assetLaptopCategory();
return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Asus')->first() ?? Manufacturer::factory()->asus();
@ -191,7 +191,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Spectre',
'category_id' => function () {
return Category::find(1) ?? Category::factory()->assetLaptopCategory();
return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'HP')->first() ?? Manufacturer::factory()->hp();
@ -210,7 +210,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Yoga 910',
'category_id' => function () {
return Category::find(1) ?? Category::factory()->assetLaptopCategory();
return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Lenovo')->first() ?? Manufacturer::factory()->lenovo();
@ -229,7 +229,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'iMac Pro',
'category_id' => function (){
return Category::find(2) ?? Category::factory()->assetDesktopCategory();
return Category::where('name', 'Desktops')->first() ?? Category::factory()->assetDesktopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
@ -248,7 +248,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Lenovo Intel Core i5',
'category_id' => function () {
return Category::find(2) ?? Category::factory()->assetDesktopCategory();
return Category::where('name', 'Desktops')->first() ?? Category::factory()->assetDesktopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Lenovo')->first() ?? Manufacturer::factory()->lenovo();
@ -267,7 +267,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'OptiPlex',
'category_id' => function (){
return Category::find(2) ?? Category::factory()->assetDesktopCategory();
return Category::where('name', 'Desktops')->first() ?? Category::factory()->assetDesktopCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Dell')->first() ?? Manufacturer::factory()->dell();
@ -287,7 +287,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'SoundStation 2',
'category_id' => function () {
return Category::find(6) ?? Category::factory()->assetVoipCategory();
return Category::where('name', 'VOIP Phones')->first() ?? Category::factory()->assetVoipCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Polycom')->first() ?? Manufacturer::factory()->polycom();
@ -305,7 +305,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Polycom CX3000 IP Conference Phone',
'category_id' => function () {
return Category::find(6) ?? Category::factory()->assetVoipCategory();
return Category::where('name', 'VOIP Phones')->first() ?? Category::factory()->assetVoipCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Polycom')->first() ?? Manufacturer::factory()->polycom();
@ -323,7 +323,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'iPad Pro',
'category_id' => function () {
return Category::find(3) ?? Category::factory()->assetTabletCategory();
return Category::where('name', 'Tablets')->first() ?? Category::factory()->assetTabletCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
@ -341,7 +341,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Tab3',
'category_id' => function () {
return Category::find(3) ?? Category::factory()->assetTabletCategory();
return Category::where('name', 'Tablets')->first() ?? Category::factory()->assetTabletCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Lenovo')->first() ?? Manufacturer::factory()->lenovo();
@ -359,7 +359,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'iPhone 11',
'category_id' => function () {
return Category::find(4) ?? Category::factory()->assetMobileCategory();
return Category::where('name', 'Mobile Phones')->first() ?? Category::factory()->assetMobileCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
@ -378,7 +378,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'iPhone 12',
'category_id' => function () {
return Category::find(4) ?? Category::factory()->assetMobileCategory();
return Category::where('name', 'Mobile Phones')->first() ?? Category::factory()->assetMobileCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
@ -397,7 +397,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Ultrafine 4k',
'category_id' => function () {
return Category::find(5) ?? Category::factory()->assetDisplayCategory();
return Category::where('name', 'Displays')->first() ?? Category::factory()->assetDisplayCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'LG')->first() ?? Manufacturer::factory()->lg();
@ -415,7 +415,7 @@ class AssetModelFactory extends Factory
return [
'name' => 'Ultrasharp U2415',
'category_id' => function () {
return Category::find(5) ?? Category::factory()->assetDisplayCategory();
return Category::where('name', 'Displays')->first() ?? Category::factory()->assetDisplayCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Dell')->first() ?? Manufacturer::factory()->dell();

View file

@ -2,6 +2,8 @@
namespace Database\Factories;
use App\Models\Category;
use App\Models\Company;
use App\Models\Location;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -32,9 +34,7 @@ class ComponentFactory extends Factory
{
return [
'name' => $this->faker->text(20),
'category_id' => function () {
return \App\Models\Category::factory()->create()->id;
},
'category_id' => Category::factory(),
'location_id' => function () {
return Location::first() ?? Location::factory();
},
@ -44,9 +44,7 @@ class ComponentFactory extends Factory
'purchase_date' => $this->faker->dateTime(),
'purchase_cost' => $this->faker->randomFloat(2),
'min_amt' => $this->faker->numberBetween($min = 1, $max = 2),
'company_id' => function () {
return \App\Models\Company::factory()->create()->id;
},
'company_id' => Company::factory(),
];
}
@ -55,7 +53,9 @@ class ComponentFactory extends Factory
return $this->state(function () {
return [
'name' => 'Crucial 4GB DDR3L-1600 SODIMM',
'category_id' => 13,
'category_id' => function () {
return Category::where('name', 'RAM')->first() ?? Category::factory()->componentRamCategory();
},
'qty' => 10,
'min_amt' => 2,
'location_id' => 3,
@ -69,7 +69,9 @@ class ComponentFactory extends Factory
return $this->state(function () {
return [
'name' => 'Crucial 8GB DDR3L-1600 SODIMM Memory for Mac',
'category_id' => 13,
'category_id' => function () {
return Category::where('name', 'RAM')->first() ?? Category::factory()->componentRamCategory();
},
'qty' => 10,
'min_amt' => 2,
];
@ -81,7 +83,9 @@ class ComponentFactory extends Factory
return $this->state(function () {
return [
'name' => 'Crucial BX300 120GB SATA Internal SSD',
'category_id' => 12,
'category_id' => function () {
return Category::where('name', 'HDD/SSD')->first() ?? Category::factory()->componentHddCategory();
},
'qty' => 10,
'min_amt' => 2,
];
@ -93,7 +97,9 @@ class ComponentFactory extends Factory
return $this->state(function () {
return [
'name' => 'Crucial BX300 240GB SATA Internal SSD',
'category_id' => 12,
'category_id' => function () {
return Category::where('name', 'HDD/SSD')->first() ?? Category::factory()->componentHddCategory();
},
'qty' => 10,
'min_amt' => 2,
];

View file

@ -2,6 +2,7 @@
namespace Database\Factories;
use App\Models\Category;
use App\Models\Manufacturer;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -49,7 +50,9 @@ class ConsumableFactory extends Factory
return $this->state(function () {
return [
'name' => 'Cardstock (White)',
'category_id' => 10,
'category_id' => function () {
return Category::where('name', 'Printer Paper')->first() ?? Category::factory()->consumablePaperCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Avery')->first() ?? Manufacturer::factory()->avery();
},
@ -65,7 +68,9 @@ class ConsumableFactory extends Factory
return $this->state(function () {
return [
'name' => 'Laserjet Paper (Ream)',
'category_id' => 10,
'category_id' => function () {
return Category::where('name', 'Printer Paper')->first() ?? Category::factory()->consumablePaperCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Avery')->first() ?? Manufacturer::factory()->avery();
},
@ -80,7 +85,9 @@ class ConsumableFactory extends Factory
return $this->state(function () {
return [
'name' => 'Laserjet Toner (black)',
'category_id' => 11,
'category_id' => function () {
return Category::where('name', 'Printer Ink')->first() ?? Category::factory()->consumableInkCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'HP')->first() ?? Manufacturer::factory()->hp();
},

View file

@ -72,7 +72,9 @@ class LicenseFactory extends Factory
'seats' => 10,
'purchase_order' => '13503Q',
'maintained' => true,
'category_id' => 14,
'category_id' => function () {
return Category::where('name', 'Graphics Software')->first() ?? Category::factory()->licenseGraphicsCategory();
},
];
return $data;
@ -89,7 +91,9 @@ class LicenseFactory extends Factory
},
'purchase_cost' => '29.99',
'seats' => 10,
'category_id' => 14,
'category_id' => function () {
return Category::where('name', 'Graphics Software')->first() ?? Category::factory()->licenseGraphicsCategory();
},
];
return $data;
@ -106,7 +110,9 @@ class LicenseFactory extends Factory
},
'purchase_cost' => '199.99',
'seats' => 10,
'category_id' => 14,
'category_id' => function () {
return Category::where('name', 'Graphics Software')->first() ?? Category::factory()->licenseGraphicsCategory();
},
];
@ -124,7 +130,9 @@ class LicenseFactory extends Factory
},
'purchase_cost' => '49.99',
'seats' => 20,
'category_id' => 15,
'category_id' => function () {
return Category::where('name', 'Office Software')->first() ?? Category::factory()->licenseOfficeCategory();
},
];
return $data;