*/ class ProductFactory extends Factory { /** @var class-string */ protected $model = Product::class; /** * Define the model's default state. * * @return array */ public function definition(): array { $base = [ 'category_id' => Category::factory()->state(['level' => 'category']), 'name' => mb_substr(fake()->name(), 0, 120), 'slug' => fake()->unique()->slug(), 'sku' => strtoupper(fake()->unique()->bothify('???-#####')), 'status' => fake()->randomElement(ProductStatus::cases()), 'price' => fake()->randomFloat(2, 1, 100000), 'compare_at_price' => fake()->randomFloat(2, 1, 100000), 'cost_price' => fake()->randomFloat(2, 1, 100000), 'photos' => ['products/'.fake()->word().'.jpg', 'products/'.fake()->word().'.jpg'], 'video_url' => fake()->url(), 'description' => fake()->paragraph(), 'size_chart' => 'products/'.fake()->word().'.pdf', 'is_featured' => false, 'published_at' => fn (array $attributes) => \Illuminate\Support\enum_value($attributes['status'] ?? null) === 'active' ? fake()->dateTime() : null, 'published_by' => fn (array $attributes) => \Illuminate\Support\enum_value($attributes['status'] ?? null) === 'active' ? (User::query()->inRandomOrder()->value('id') ?? User::factory()) : null, 'specs' => ['colour' => fake()->safeColorName(), 'material' => fake()->word()], ]; // The values above satisfy the schema. Rules the schema cannot state — a // child row must belong to a parent of one type, a synced record may not // be minted — live in `database/factories/Domain/product.php`, owned by // the project: an array merged over the base, or a Closure(array): array. // Never touched by the generator, so it survives --force. The same // hook as `tests/Payloads/product.php` for the generated test payload. $domain = base_path('database/factories/Domain/product.php'); if (is_file($domain)) { /** @var array|\Closure(array): array $rules */ $rules = require $domain; $base = $rules instanceof \Closure ? $rules($base) : array_merge($base, $rules); } return $base; } }