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ế.
Factories generate fake data for testing: php artisan make:factory UserFactory.
- Define:
public function definition() { return ["name" => $this->faker->name, "email" => $this->faker->unique()->safeEmail]; }then useUser::factory()->create()orUser::factory(10)->create()for 10 records. - Seeders populate database:
php artisan make:seeder UserSeederthen call factoryUser::factory(100)->create()and run withphp artisan db:seed. - Essential for development (fake data) and testing (consistent test data).
- Factories use Faker library for realistic random data.