Laravel 5.4 #13 : Seeders

Seeders make it easy to create fake test data. Exactly, what can be done with the seeders is to generate fake data and save it in the database, with this we will not have to go inserting one by one the test data in the database.

First we have to create the file for the seeder, we will do it with an artisan command:

And put this code in the run() function:

This code what it does is generating 5 users and assigning to each user 30 posts.

And add this line in DatabaseSeeder.php in the run() function, to run when we run the seeders:

To indicate what type of data is to be inserted in the database, we have to put it in the file  database/factories/ModelFactory.php :

The code is quite descriptive. If you want to see all the types of data that are admitted you can see them here.

To run the seeders, we will have to do it through the command line:

 

If everything is OK, we would already have the information in our database and that’s how we can save a lot of time when creating test data.

-

– Seeders: https://laravel.com/docs/5.4/seeding