Kritim Yantra
Apr 20, 2025
Introduction
Laravel is a powerful PHP framework that simplifies web application development. One of its most useful features is Eloquent Factories, which allow developers to generate fake data for testing and database seeding. In Laravel 12, factories have been further improved to make them more flexible and intuitive.
In this guide, we’ll cover:
By the end of this blog, you’ll have a solid understanding of how to use Laravel 12 Factories effectively in your projects.
Factories in Laravel are classes that define how fake data should be generated for your Eloquent models. Instead of manually creating test records, factories allow you to:
Factories are commonly used in:
php artisan db:seed
)In Laravel 12, factories are typically generated using Artisan commands. Let’s create a factory for a Post
model.
Run the following command:
php artisan make:factory PostFactory
This creates a new file in database/factories/PostFactory.php
.
Inside the factory, you define how data should be generated using Faker, a PHP library for fake data generation.
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory
{
protected $model = Post::class;
public function definition()
{
return [
'title' => $this->faker->sentence(),
'content' => $this->faker->paragraph(),
'published_at' => $this->faker->dateTime(),
];
}
}
Here:
title
generates a random sentence.content
generates a paragraph.published_at
assigns a random datetime.Once a factory is defined, you can use it to create model instances.
$post = Post::factory()->create();
This inserts a new Post
into the database with fake data.
$posts = Post::factory()->count(10)->create();
This creates 10 posts in the database.
You can customize specific fields:
$post = Post::factory()->create([
'title' => 'Custom Title',
]);
This creates a post with a custom title while other fields remain randomized.
Sometimes, you need different variations of a model. Laravel allows you to define states for this purpose.
// Inside PostFactory.php
public function published()
{
return $this->state(function (array $attributes) {
return [
'published_at' => now(),
];
});
}
public function draft()
{
return $this->state(function (array $attributes) {
return [
'published_at' => null,
];
});
}
Now you can generate posts in different states:
$publishedPost = Post::factory()->published()->create();
$draftPost = Post::factory()->draft()->create();
Factories can also define relationships between models. Let’s say a Post
belongs to a User
.
// UserFactory.php
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
];
}
// PostFactory.php
public function definition()
{
return [
'user_id' => User::factory(),
'title' => $this->faker->sentence(),
'content' => $this->faker->paragraph(),
];
}
Now, when you create a post, a user is automatically generated:
$post = Post::factory()->create();
If you want one user to have multiple posts:
$user = User::factory()
->has(Post::factory()->count(3))
->create();
This creates 1 user with 3 posts.
Factories are commonly used in DatabaseSeeder.php
to populate your database with test data.
// database/seeders/DatabaseSeeder.php
public function run()
{
\App\Models\User::factory(10)->create()->each(function ($user) {
$user->posts()->saveMany(
\App\Models\Post::factory(5)->make()
);
});
}
Run the seeder with:
php artisan db:seed
This creates 10 users, each with 5 posts.
make()
instead of create()
when you don’t need to persist data to the database.Laravel 12 Factories are an essential tool for generating test data efficiently. They help streamline development by:
By mastering factories, you can speed up your workflow and ensure your tests and development environments have realistic data.
Now that you understand Laravel Factories, try implementing them in your next project!
Happy coding! 🎉
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google