Improve factories for location relationships

This commit is contained in:
Marcus Moore 2023-03-15 18:31:08 -07:00
parent b944945377
commit dfd6cf0d29
No known key found for this signature in database
10 changed files with 85 additions and 46 deletions

View file

@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Models\Category;
use App\Models\Location;
use App\Models\Manufacturer;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -37,7 +38,7 @@ class AccessoryFactory extends Factory
return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin();
},
'model_number' => $this->faker->numberBetween(1000000, 50000000),
'location_id' => rand(1, 5),
'location_id' => Location::factory(),
];
}

View file

@ -308,8 +308,8 @@ class AssetFactory extends Factory
'model_id' => function () {
return AssetModel::where('name', 'Macbook Pro 13')->first() ?? AssetModel::factory()->mbp13Model();
},
'assigned_to' => \App\Models\Location::factory()->create()->id,
'assigned_type' => \App\Models\Location::class,
'assigned_to' => Location::factory(),
'assigned_type' => Location::class,
];
});
}

View file

@ -35,9 +35,7 @@ class ComponentFactory extends Factory
return [
'name' => $this->faker->text(20),
'category_id' => Category::factory(),
'location_id' => function () {
return Location::first() ?? Location::factory();
},
'location_id' => Location::factory(),
'serial' => $this->faker->uuid,
'qty' => $this->faker->numberBetween(3, 10),
'order_number' => $this->faker->numberBetween(1000000, 50000000),
@ -58,7 +56,7 @@ class ComponentFactory extends Factory
},
'qty' => 10,
'min_amt' => 2,
'location_id' => 3,
'location_id' => Location::factory(),
];
});
}

View file

@ -2,6 +2,7 @@
namespace Database\Factories;
use App\Models\Location;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -34,7 +35,7 @@ class DepartmentFactory extends Factory
'user_id' => function () {
return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin();
},
'location_id' => rand(1, 5),
'location_id' => Location::factory(),
];
}

View file

@ -23,8 +23,6 @@ class LocationFactory extends Factory
'currency' => $this->faker->currencyCode(),
'zip' => $this->faker->postcode(),
'image' => rand(1, 9).'.jpg',
];
}
}

View file

@ -3,6 +3,7 @@
namespace Database\Seeders;
use App\Models\Accessory;
use App\Models\Location;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@ -14,10 +15,17 @@ class AccessorySeeder extends Seeder
{
Accessory::truncate();
DB::table('accessories_users')->truncate();
Accessory::factory()->count(1)->appleUsbKeyboard()->create();
Accessory::factory()->count(1)->appleBtKeyboard()->create();
Accessory::factory()->count(1)->appleMouse()->create();
Accessory::factory()->count(1)->microsoftMouse()->create();
if (! Location::count()) {
$this->call(LocationSeeder::class);
}
$locationIds = Location::all()->pluck('id');
Accessory::factory()->count(1)->appleUsbKeyboard()->create(['location_id' => $locationIds->random()]);
Accessory::factory()->count(1)->appleBtKeyboard()->create(['location_id' => $locationIds->random()]);
Accessory::factory()->count(1)->appleMouse()->create(['location_id' => $locationIds->random()]);
Accessory::factory()->count(1)->microsoftMouse()->create(['location_id' => $locationIds->random()]);
$src = public_path('/img/demo/accessories/');
$dst = 'accessories'.'/';

View file

@ -12,7 +12,5 @@ class ActionlogSeeder extends Seeder
Actionlog::truncate();
Actionlog::factory()->count(300)->assetCheckoutToUser()->create();
Actionlog::factory()->count(100)->assetCheckoutToLocation()->create();
}
}

View file

@ -3,6 +3,7 @@
namespace Database\Seeders;
use App\Models\Asset;
use App\Models\Location;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@ -13,31 +14,38 @@ class AssetSeeder extends Seeder
public function run()
{
Asset::truncate();
Asset::factory()->count(1000)->laptopMbp()->create();
Asset::factory()->count(50)->laptopMbpPending()->create();
Asset::factory()->count(50)->laptopMbpArchived()->create();
Asset::factory()->count(50)->laptopAir()->create();
Asset::factory()->count(5)->laptopSurface()->create();
Asset::factory()->count(5)->laptopXps()->create();
Asset::factory()->count(5)->laptopSpectre()->create();
Asset::factory()->count(5)->laptopZenbook()->create();
Asset::factory()->count(3)->laptopYoga()->create();
Asset::factory()->count(30)->desktopMacpro()->create();
Asset::factory()->count(30)->desktopLenovoI5()->create();
Asset::factory()->count(30)->desktopOptiplex()->create();
if (! Location::count()) {
$this->call(LocationSeeder::class);
}
Asset::factory()->count(5)->confPolycom()->create();
Asset::factory()->count(2)->confPolycomcx()->create();
$locationIds = Location::all()->pluck('id');
Asset::factory()->count(12)->tabletIpad()->create();
Asset::factory()->count(4)->tabletTab3()->create();
Asset::factory()->count(1000)->laptopMbp()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(50)->laptopMbpPending()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(50)->laptopMbpArchived()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(50)->laptopAir()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(5)->laptopSurface()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(5)->laptopXps()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(5)->laptopSpectre()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(5)->laptopZenbook()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(3)->laptopYoga()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(27)->phoneIphone11()->create();
Asset::factory()->count(40)->phoneIphone12()->create();
Asset::factory()->count(30)->desktopMacpro()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(30)->desktopLenovoI5()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(30)->desktopOptiplex()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(10)->ultrafine()->create();
Asset::factory()->count(10)->ultrasharp()->create();
Asset::factory()->count(5)->confPolycom()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(2)->confPolycomcx()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(12)->tabletIpad()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(4)->tabletTab3()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(27)->phoneIphone11()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(40)->phoneIphone12()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(10)->ultrafine()->create(['rtd_location_id' => $locationIds->random()]);
Asset::factory()->count(10)->ultrasharp()->create(['rtd_location_id' => $locationIds->random()]);
$del_files = Storage::files('assets');
foreach ($del_files as $del_file) { // iterate files

View file

@ -4,6 +4,7 @@ namespace Database\Seeders;
use App\Models\Company;
use App\Models\Component;
use App\Models\Location;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
@ -20,9 +21,27 @@ class ComponentSeeder extends Seeder
$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()]);
if (! Location::count()) {
$this->call(LocationSeeder::class);
}
$locationIds = Location::all()->pluck('id');
Component::factory()->ramCrucial4()->create([
'company_id' => $companyIds->random(),
'location_id' => $locationIds->random(),
]);
Component::factory()->ramCrucial8()->create([
'company_id' => $companyIds->random(),
'location_id' => $locationIds->random(),
]);
Component::factory()->ssdCrucial120()->create([
'company_id' => $companyIds->random(),
'location_id' => $locationIds->random(),
]);
Component::factory()->ssdCrucial240()->create([
'company_id' => $companyIds->random(),
'location_id' => $locationIds->random(),
]);
}
}

View file

@ -3,6 +3,7 @@
namespace Database\Seeders;
use App\Models\Department;
use App\Models\Location;
use Illuminate\Database\Seeder;
class DepartmentSeeder extends Seeder
@ -10,11 +11,18 @@ class DepartmentSeeder extends Seeder
public function run()
{
Department::truncate();
Department::factory()->count(1)->hr()->create(); // 1
Department::factory()->count(1)->engineering()->create(); // 2
Department::factory()->count(1)->marketing()->create(); // 3
Department::factory()->count(1)->client()->create(); // 4
Department::factory()->count(1)->product()->create(); // 5
Department::factory()->count(1)->silly()->create(); // 6
if (! Location::count()) {
$this->call(LocationSeeder::class);
}
$locationIds = Location::all()->pluck('id');
Department::factory()->count(1)->hr()->create(['location_id' => $locationIds->random()]);
Department::factory()->count(1)->engineering()->create(['location_id' => $locationIds->random()]);
Department::factory()->count(1)->marketing()->create(['location_id' => $locationIds->random()]);
Department::factory()->count(1)->client()->create(['location_id' => $locationIds->random()]);
Department::factory()->count(1)->product()->create(['location_id' => $locationIds->random()]);
Department::factory()->count(1)->silly()->create(['location_id' => $locationIds->random()]);
}
}