feat: Implement Warehouse and EggCollectionItem factories, updating EggCollection factory and seeder for item and warehouse management.

This commit is contained in:
Yoga Pangestu 2026-03-09 13:57:04 +07:00
parent 8813249154
commit 5c068017b2
4 changed files with 60 additions and 3 deletions

View File

@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Models\EggCollection;
use App\Models\Warehouse;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
@ -18,8 +19,7 @@ public function definition(): array
return [
'production_date' => $dateTime,
'total_eggs' => fake()->numberBetween(500, 1500),
'total_broken_eggs' => fake()->numberBetween(0, 100),
'warehouse_id' => Warehouse::first()?->id ?? Warehouse::factory(),
'notes' => $this->faker->optional(0.4)->sentence(),
];
}

View File

@ -0,0 +1,28 @@
<?php
namespace Database\Factories;
use App\Models\EggCollectionItem;
use App\Models\Unit;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\EggCollectionItem>
*/
class EggCollectionItemFactory extends Factory
{
protected $model = EggCollectionItem::class;
public function definition(): array
{
$unit = Unit::whereIn('alias', ['butir', 'kg'])->inRandomOrder()->first()
?? Unit::where('alias', 'butir')->first()
?? Unit::factory();
return [
'unit_id' => $unit->id,
'quantity' => $unit->alias === 'butir' ? fake()->numberBetween(50, 500) : fake()->randomFloat(2, 5, 50),
'is_broken' => fake()->boolean(10), // 10% chance of being broken
];
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Database\Factories;
use App\Models\Warehouse;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Warehouse>
*/
class WarehouseFactory extends Factory
{
protected $model = Warehouse::class;
public function definition(): array
{
return [
'name' => $this->faker->company().' Warehouse',
];
}
}

View File

@ -3,6 +3,7 @@
namespace Database\Seeders;
use App\Models\EggCollection;
use App\Models\EggCollectionItem;
use Illuminate\Database\Seeder;
class EggCollectionSeeder extends Seeder
@ -12,6 +13,13 @@ class EggCollectionSeeder extends Seeder
*/
public function run(): void
{
EggCollection::factory()->count(30)->create();
EggCollection::factory()
->count(30)
->create()
->each(function ($collection) {
EggCollectionItem::factory()->count(rand(1, 3))->create([
'egg_collection_id' => $collection->id,
]);
});
}
}