Factory sinh dữ liệu giả để test, tạo bằng php artisan make:factory UserFactory:
php
public function definition() {
return [
"name" => $this->faker->name,
"email" => $this->faker->unique()->safeEmail,
];
}
// dùng
User::factory()->create(); // 1 bản ghi
User::factory(10)->create(); // 10 bản ghiSeeder dùng để populate database: php artisan make:seeder UserSeeder, bên trong gọi User::factory(100)->create() rồi chạy php artisan db:seed.
Đây là bộ đôi thiết yếu cho development (dữ liệu giả) lẫn testing (dữ liệu nhất quán). Factory dựa trên thư viện Faker nên dữ liệu ngẫu nhiên vẫn trông thật.