In the past, I have published the article How to Seed the Database in PHP using Faker Library. While researching this topic, I found that Laravel also uses the same library for seeding the database. As I have written an article on this topic for PHP, I decided to write a post on database seeding in Laravel. Unlike plain PHP, Laravel uses its own flow to seed the database.
What is Database Seeding?
Database seeding is the process of filling your database tables with the test data programmatically. With this approach, one can insert thousands of fake entries into the database within a few minutes. It is an efficient way rather than doing it manually. You may want dummy data to test out your application and seeding the database programmatically saves you a ton of time.
Faker Library
Faker is an open-source library that generates fake data for your application’s database. Using Faker, one can insert thousands of entries in the database in a couple of minutes. This library saves a boring data entry job for developers. You can get almost any kind of data you want. The library generates a fake email, name, mobile number, words, sentences, paragraphs, random numbers, etc. You may read more about the library in the documentation.
Under the hood, Laravel uses a Faker library for database seeding. Laravel installs this library at its core. You don’t need to install it separately. All you need to do is follow the steps for the seeding and you’re done.
Getting Started
To get started, I’ll create a customers
table with the column’s name, email, identification_number, and bio. We will see the process of seeding the single table. In the same way, one can seed as many tables as they want.
Create a migration for customers
table using the command:
php artisan make:migration create_customers_table
Open the migration file and add the columns to the up()
method as shown below.
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('identification_number');
$table->text('bio');
$table->timestamps();
});
}
Run the migrate command which will create a customers
table in your database.
php artisan migrate
Next, create a Customer
model which maps to the customers
table.
php artisan make:model Customer
Database Seeding in Laravel
Seeding a database in Laravel requires you to perform the following steps.
- Writing seeders
- Create a model factory
- Run seeders
Writing Seeders
If you look at the Laravel filesystem, you would find the database/seeders
directory. All seed classes are located inside this directory. A seed class has the run()
method where you can write logic to insert the data in the table.
Make a seed class CustomerSeeder
using the command:
php artisan make:seeder CustomerSeeder
After executing this command, you should see the CustomerSeeder.php
file placed under the database/seeders
folder. Open this file and to the run()
function add the below code.
public function run()
{
Customer::factory()->count(50)->create();
}
Here I am calling the Customer factory which I’ll create in the next step. Notice I passed the value as 50 to the count()
method. It means 50 rows should be inserted into the customers
table.
Creating Model Factories
The code I have written in CustomerSeeder
will look for a Customer factory to get the test data. It means we have to create a model factory called CustomerFactory
. Run the below command to create the model factory.
php artisan make:factory CustomerFactory
This command will create a CustomerFactory.php
inside the database/factories
directory. Open this file, the definition()
method would generate test data as follows.
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->email(),
'identification_number' => $this->faker->randomNumber(9, true),
'bio' => $this->faker->paragraph(),
];
}
The above array returns a random name, email, 9-digit number, and text for bio.
Running Seeders
We have written the CustomerSeeder and CustomerFactory. Now run the CustomerSeeder using the command below.
php artisan db:seed --class=CustomerSeeder
After this, check your customers
table, it should have the 50 dummy entries inserted.
The above command runs a single seeder which we passed explicitly. Let’s say you want to create multiple seeders and run all of them using a single command. In such cases, you should call your seeders from the DatabaseSeeder
which is found in the same database/seeders
directory.
public function run()
{
$this->call([
CustomerSeeder::class,
// next seeder
]);
}
The user can create as many seeders as per their requirements and call them from this run()
method.
Finally, run the seeders using the command below.
php artisan db:seed
It will call all seeders from the run method and execute it.
I hope you understand database seeding in Laravel. This should help you to fill up the database with tons of entries to test your application.
Related Articles
- PayPal Payments Pro Integration in Laravel
- Create Honeypot for Battling Form Spam in Laravel
- Adding Google reCAPTCHA v3 to your Laravel Website
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
I have successfully migrated my database and run db:seed but it returning without my database, with a message “seeding database”
Hi
i see many YouTube video and visit other website but I cannot found solution for factory and seeder. But i see you website and i use you method for to create seeders and factories. My problem solve. you method is to much easy, each and every thing describe step by step. Thank you very much to solve my problem.
thanks dmin good luck
This information is very useful for me