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
<?php
namespace Database\Factories;
use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;
class ProductFactory extends Factory
{
protected $model = Product::class;
public function definition(): array
{
return [
'name' => $this->faker->unique()->word . ' ' . $this->faker->randomNumber(3, true),
'description' => $this->faker->paragraph,
'price' => $this->faker->randomFloat(2, 10, 1000),
'stock' => $this->faker->numberBetween(0, 500),
];
}
}
💡
Key Features
• Unique product names for reliable tests
• Realistic price and stock ranges
• Useful for seeding and automated tests