Seeder and Model Factory in Laravel

Tram Ho

Written by: Tran Cong Tu

Hi everybody. As you know, testing is a repetitive task in the application building process. That’s why we need a sample data set to serve that job. Instead of manually entering the manual, Laravel provided a Seeder to create data. In this article we will create a simple seeder and understand how it works.

What is a seeder?

Seeder is actually a class to create sample data for the database during application building process.

Steps to create a seeder

We go to the database / seeds / directory and we will see a file DatabaseSeeder.php , this is called the root seeder will be run when we add data, the original file content is as follows.

As we can see, in the original seeder file, there is a run () function , and if we want to add data to the database we can write query statements in this function.

However, if we add all the sample data to our database in this original seeder file, it will lead to confusion and difficult to control. In fact, we will split into small seeder files for each table, then call them into the function run () in the original seeder file. We will create a new seeder for users table.

Step 1 : Create a new seeder for users table named UsersTableSeeder.php

Step 2 : Now we have a file UsersTableSeeder.php in the database / seeds / directory . We will add query statements to create seed for users table.

Step 3 : Now we just need to call the UsersTableSeeder.php class into the run () function in the original seeder file.

Step 4 : We will have 2 ways to run.

  • Method 1: Specify the seeder class to run with the command

  • Method 2: Run all seeders that are called to the original seeder file.

So we successfully created sample data into the database. Besides, we can also use a library to create fake Faker data. This library supports creating the same data as real data, so we do not have to think about names, phone numbers or emails .. etc ..

Come here, you see everything is getting better right? So I ask a question. As in the example above with a small number of records, 2 records, we can manually add, then the number of records larger than 100 we will run 100 loops to add data. What about the number of records up to 10000 records? Do not worry! Laravel has provided us a machine that is Model Factory. Let’s learn how to operate 1 Model Factory.

Create Model Factory

We will continue the example with the users table by creating a Model Factory for it and feel it together.

Step 1 : Create the Model Factory for the users table with the name UserFactory.php

Step 2 : After completing step 1, we will get a UserFactory.php file in the database / factories / directory . Next, we will create the data, in the Model Factory, a built-in library of fake Faker data.

Step 3 : Now in the run () function of the UsersTableSeeder class we use the factory () helper function with 2 transmission numbers:

  1. Class models need to create data.
  2. Number of records to create.

So creating bulk sample data is no longer a problem to worry about, right? We can easily see that the pre-defined data structure needed to insert into the database will help us optimize the code and facilitate the process of editing or changing the number of records easily.

Conclude

So far, everyone has understood the seeder, model factory and how it works. Hope this article is interesting, as well as contribute a little knowledge to everyone. Wished everyone success.

Share the news now

Source : Viblo