Factory tạo dữ liệu giả để test: php artisan make:factory UserFactory.
- Định nghĩa:
public function definition() { return ["name" => $this->faker->name, "email" => $this->faker->unique()->safeEmail]; }rồi dùngUser::factory()->create()hoặcUser::factory(10)->create()cho 10 bản ghi. - Seeder populate database:
php artisan make:seeder UserSeederrồi gọi factoryUser::factory(100)->create()và chạy bằngphp artisan db:seed. - Thiết yếu cho development (dữ liệu giả) và testing (dữ liệu nhất quán).
- Factory dùng thư viện Faker để tạo dữ liệu ngẫu nhiên thực tế.