Laravel 5: Setting-up Testing Environment – DB Tables Used in Examples

Examples use two tables created and seeded during testing environment creation.
Sample tables below show only limited number of number of entries for demonstration purposes.

table 1: users


table 2: tasks



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

Seeding Database Testing Data


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:

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:




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:



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.


Laravel 5: Setting-up Testing Environment – Code used in examples

Code used in examples explained in detail.

First, lets see at some code that we are going to use to generate our examples.

I do everything assuming you are on Windows localhost (e.g. XAMPP) – on other OS, e.g. online LAMP server, you may need to modify my instructions to some extent.

Lets install Laravel

What I use here is slightly modified Laravel “The Quickstart” example.

Let me explain how to install it, as it may be challenging to a beginner, since creators of documentation missed a step or two.

Here is original code picked of site to which link given above goes (with some modification):

So, lets look at what’s above step by step.

Above code is going to install on your computer (localhost), or server.
Lets assume it is localhost.
First decide into which folder you want to download this example files – lets say it would be c:\mysites.
So, in Command Prompt you type: cd c:\mysites.
“quickstart” is a directory inside of c:\mysites, where you find your intermediate example files downloaded.

Now we are going into our example directory, so we can run next command.

Also in the future, to access php artisan commands, tinker, or composer from command prompt, you have to get into that directory first. In our case it is quickstart, but your may be anything you choose it to be.
You may also read these links to get more info on the installation subject:

Now, lets look at our missing steps.

Step 1:
You need to set your database and get some credentials ready for Laravel.

Step 2:
Go to main directory and find file called .env
Inside look for this code and fill it with credentials from step 1.

Now you could run next line of code and finish with data migration to your database, but wait.
Let’s first add that missing column to tasks table, which we will need in our example.

Migrations and Seeding is described in detail here.


We are done installing – lets look at example coding.
I prefer to tinker with code directly – I will use a couple of Laravel files directly to apply example code.
Model located at:

Controller located at:

So, all code testing I use here is done in these 2 files.



Laravel 5.3 does not have Tutorials (Basic, Intermediate) anymore.
Here is even better replacement.

In case that great tutorial (git repo of it)  ever disappeared, here is full zip of it: laravel5-3-example-master

Just follow installation instructions and you’ll be up in a few minutes.
Instructions are in file.
Author made it extremely easy … even for beginner.



If you are getting this error:

Try running this code in Command Prompt:

Now run seeding command again:

Sometimes you may also run this code as well to get rid of that error:

… and seeding command.
But usually, all you need is to regenerate class autoloader file using optimize command.

Just one more piece of information – how to access it from browser.

  • navigate to your example address – be it one on localhost, or one on server
  • create a profile – register
  • login using profile
  • you are there

Now, we are ready with our example skeleton.