Trung BìnhLaravel iconLaravel

Factory và Seeder trong Laravel là gì?

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ùng User::factory()->create() hoặc User::factory(10)->create() cho 10 bản ghi.
  • Seeder populate database: php artisan make:seeder UserSeeder rồi gọi factory User::factory(100)->create() và chạy bằng php 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ế.

Xem toàn bộ Laravel cùng filter theo level & chủ đề con.

Mở danh sách Laravel