ProductFactory.php
database/factories/ProductFactory.php
Eloquent Model Factory for Product
The ProductFactory generates dummy product data for testing and seeding your database.
• Ensures unique product names
• Realistic descriptions and prices
• Randomized stock values
Complete Implementation
// filepath: database/factories/ProductFactory.php <?php namespace Database\Factories; use App\Models\Product; use Illuminate\Database\Eloquent\Factories\Factory; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product> */ class ProductFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = Product::class; /** * Define the model's default state. * * @return array<string, mixed> */ public function definition(): array { return [ 'name' => $this->faker->unique()->word . ' ' . $this->faker->randomNumber(3, true), // Ensures unique name 'description' => $this->faker->paragraph, 'price' => $this->faker->randomFloat(2, 10, 1000), // Price between 10.00 and 1000.00 'stock' => $this->faker->numberBetween(0, 500), // Stock between 0 and 500 ]; } }
💡
Key Features
• Unique product names for reliable tests
• Realistic price and stock ranges
• Useful for seeding and automated tests