Laravel 5: Setting-up Testing Environment – Seeding Database Testing Data

Seeding Database Testing Data

MIGRATIONS – CREATE DATABASE TABLE

In order to seed, we need database table.

I assume, that database itself is ready, as we are already covered that in previous posts.

To get new table ready I will use builtin Migrations.
Read also about practical issues with migrations here.

Laravel migrations are like database version control.
They have date, number and name, like ‘create_tasks_table’.


Step 1: create migration boilerplate file – it will be empty one.

We will create one using terminal (Command Prompt) and Laravel Artisan CLI helper.
Artisan commands can be accessed once you navigate your Command Prompt to directory, where you installed your Laravel.

Above will create table::tasks:
Migration files are stored in this directory:

… and have long names:

 


Step #2: add some working code to migration boilerplate.

More about how to create columns, as I did in what I call ‘working code’, can be found here.

Each migration boilerplate class has two methods:

  1. up() – to create something
  2. down() – to delete (rollback) something

We are creating table::tasks with columns

  • id
  • user_id (foreign key for table::users)
  • name (I use it for numeric entry)
  • details (filled with random string)
  • timestamps, which create 2 columns: created_at, updated_at

So, above code will create 6 columns altogether.

 


step #3: run code we prepared in steps: 1 and 2

Lets run our migration and create our empty table.

You can create a lot of tables and run them all at once using this code:

Note:
Laravel does not allows to select which migration file you want to run.
You have to run them all.
These already run will be ignored.

Now, you should have this empty table table ready in your database:

 


SEED CREATED TABLE WITH TESTING DATA

 

step #1: create seeding factory

Open this file – it can hold all your factories:

Or you may create other factories within above directory, e.g. CommentFactory.php etc.
They will be autoloaded,

Now add this code to it:

Above code will fill out table:

  1. id column is auto-created so we need to nothing
  2. user_id column: would enter number 51 – id of user 51
  3. name column: would create random number – 3 digits long
  4. details column: would create 3 words long sentence from Lorem Ipsum
  5. rest of columns are auto-created.

Columns: name and details use Faker library stored in directory:

You can read more about this great seeding directory here.
It allows to create emails, user names, ISBN and many more goodies.

We can add a lot of factories into ModelFactory.php file.

So, our factory is ready to manufacture.

 


step #2: create seeder for our task table

This seeded file can be created by hand, but also you can use Artisan CLI code, that will do this for you:

You will find your seeder boilerplate in this directory:

Add to method run() this code:

Above code will tell Laravel to use model App\Task to seed 50 rows of data to table::tasks.

 


step #3: register seeder with seeder switchboard

In order to tell Laravel (and model App\Task) that you have created a new seeded, you have to add it to DatabaseSeeder class, which serves as seeder directory for all factories.

You can find this file here:

And the code to add to method run() is this:

What above code tells Laravel is to run TaskTableSeeder, if seeding is requested via Artisan CLI.

We can add a lot of seeders into that method.

 


step #4: run seeding

Now we have all stuff in place and we can run seeding using Artisan CLI.

We can run all seeders registered in DatabaseSeeder:

… or we can select only one seeder:

 


Result:

If you did everything right, you should get your table populated with 50 rows of data.
You should get something like this sample:

 


Same way you can seed any other table with testing data in your database.
Just use above schema and Faker library … and you are set in minutes.