How to use Factories and Fakers in Laravel ?
Hello LaraDevs !
Laravel has many option to make your development process easier. Sometimes, you use this solutions in most of your projects because they are really useful for developers. We will learn a new feature with this article what general laravel developers use it in their projects.
In this article, you will basically learn why we use factories and fakers. Also we will use seeders with factories to seed our table/tables. At end of this article, We will have learned how we can fill our tables with fake rows to use factory, fakers and seeders.
What is Laravel Factory ?
When we are developing a new project or adding a new feature to our current projects, we need to fake datas which is easy to create and test it. Factories helping us at this point.
Factories are creating dummy data at our databases. Also you can use them with seeders to seed database with these fake rows. You can create hundreds even thousands of new row with factories.
What is Laravel Faker ?
Sometimes we need only fake data at our database and we really don’t want to waste time to thinking about that. Then faker class helps us. For example, you have a column in your users database and you need to create hundreds of new users with any name. Then you can use faker class to create many option. Also fakers have many options for your needs at database. Faker can create any address, number, postal code or anything you need.
Why we use factories and Fakers ?
Generally, we mentioned about these functions benefits in a project. To summarize everything, we can create many new rows in our tables in a few seconds and we can save really remarkable time with this way. Laravel is a framework, it’s mean is you do not need to think about similar problem like fake data. You can focus to your real tasks with similar ways.
Let’s create then a users table with migration.
If you run following command in your terminal, artisan will create a new model named as User and migration which has relation with this model.
php artisan make:model User -m
Our migration will be like following :
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('phone')->nullable();
$table->string('email');
$table->string('password');
$table->dateTime('email_verified_at')->nullable();
$table->text('reset_password_code')->nullable();
$table->dateTime('birth_date')->nullable();
$table->timestamps();
});
We have a few required columns like first_name, last_name… At this point, we will create a new factory (Also laravel create factory with model, you need to check it first.) to have fake data. To having that, we will use also factory class.
If you run following command in your terminal, you will have a new factory which is related to User model.
php artisan make:factory UserFactory --model=User
In your new factory, you will see following line as added. It’s mean is, this factory will create data for User model. (If you use Laravel 9, you will see the factory at database/factory/UserFactory)
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
You will see definition class in this factory class. We will describe everything at this function. We can use directly fakers in this function.
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'phone' => $this->faker->phoneNumber,
'email' => $this->faker->safeEmail,
'password' => $this->faker->password,
'email_verified_at' => $this->faker->dateTime(),
'reset_password_code' => $this->faker->numberBetween(-10000, 10000),
'birth_date' => $this->faker->dateTime(),
];
}
On the other hand, you can use factories really functional for your needs. In this article, we will not interest to details but factories has really perfect functionality about that.
As you can see below, we used factory class to creating first name, last name, phone number, email, password, date etc.
Let’s use factory with a seeder. You can directly create a new seeder if you run following command at your terminal.
php artisan make:seed UserTableSeeder
Seeder’s content will be so simple as following example.
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::factory()->count(50)->create();
}
}
Everything is ready to fill our users table with fake datas. As you can see in our seeder, we used count(xx) property. It’s mean is factory will create 50 new rows in our table. Also you can increase or decrease that parameter or you can use it more functional as your needs.
Right now, we can seed our database. If you run following command, you will see 50 different rows in your users table.
php artisan db:seed --class=UserTableSeeder
Let’s discuss about that with your comments ! Is it helpful or not ?
Thank you so much !
Resources :
Enver ŞANLI
Developer, Social Thinker & Farmer