![Laravel 10 Generate Test or Dummy Data using Factory Tinker Laravel 10 Generate Test or Dummy Data using Factory Tinker](https://webthestuff.com/upload/post/laravel-10-generate-test-or-dummy-data-using-factory-tinker.png)
Factory and Tinker, you can quickly generate dummy data for your application without manually creating records through database interactions or using third-party tools. This saves time during development and testing phases.
php artisan tinker
User::factory()->count(5)->create()
Create Custom Factory:
app\Models\Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'slug', 'detail'
];
}
now let's create our custom factory using bellow command:
php artisan make:factory ProductFactory --model=Product
database\factories\ProductFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Product;
/**
* @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
*/
public function definition(): array
{
return [
'name' => $this->faker->name,
'slug' => Str::slug($this->faker->name),
'detail' => $this->faker->text,
];
}
}
Generate Dummy Product:
php artisan tinker
Product::factory()->count(500)->create()