When developing an application with Laravel, having consistent test data is important. Whenever you create a new model with a factory via Artisan (php artisan make:model), you have the option of creating a factory alongside it by passing the -f option.
Factories are where you define the shape of a model (which fields get which type of fake data).
Laravel ships with a User factory out of the box at database/factories/UserFactory.php, and it’s a great reference:
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('password'),
'remember_token' => Str::random(10),
];
}
}Each key in that array maps to a column on your model’s table, and the fake() helper gives you access to the Faker library that generates realistic names, emails, addresses, phone numbers, and just about anything else you can think of.
Tomorrow, we’ll talk about how you can spin up a whole database full of data using factories so you can have some realistic test scenarios.




