Home Database Learn database seeding in Laravel 5.8 from scratch

Learn database seeding in Laravel 5.8 from scratch


What is Database Seeding?

Database seeding is basically a process of loading test data into your application, the data can be anything according to your project requirements.

Database seeding is a process where an initial set of data is loaded into a database when application is being installed

Also you can even seed usable data for example sometime we need predefined tables with default values so we can utilise then in the application.

Database seeding is very power full feature from laravel application it helps a lot.

In this tutorial I will provide a complete guide on database seeding starting from creating application to seeding the data.

Create new Laravel Application

Create new laravel application using laravel installer or composer. Or you can use your existing laravel application as well.

Execute following command to create new project:

OR use composer

Database setup

We will use MySql for our database storage go ahead and login to your mysql server and create database.

Next open your project into your favourite code editor and update .env file to configure database settings as showing below:

Please make sure to change database username, password and database name to match with your mysql database user and the database that you have created.

Generate new Migration File and Model Class

We will need a new table to store data and eloquent model class associated with it.

Generate Files:

The following command will create both the file at the same time.

Open your terminal and execute following command from the root of your application:

Setup Migration Table and Model Class:

Now go ahed and edit migration file to have few more columns to the database table.

Now Update the Post model class to have fillable attribute so that it will allow to insert new records

Migrate the database:

Now that we have our migration file ready we can migrate it to create new table:

Execute following artisan command to migrate the database

You will see following result if you’re migrating the database first time like I did if not then you should see the specific result to the migrated file.

Generate Database Seeder

Laravel stores all the seed classes inside database/seeds directory. where you will find DatabaseSeeder class define to run all the seeding operation, we can easily use call method to execute other seed classed from the run method.

To Generate a seeder class we can use make:seeder artisan command it will generate new seeder class file inside database/seeds directory.

Execute following command to create Post table Seeder:

You can locate PostsTableSeeder.php file and as showing below:

From the above seeder class we are asking to create a record with random values.

Now Register new seeder into DatabaseSeeder class:

Running Database Seeders

Our next is run newly created database seeder, use following command to execute database seeders.

Note – Sometime if you running into issue while executing seeder you may need to regenerate composers autoloader using following command:

Run seeder with default db:seed artisan command:

We can also run specific seeder class by seeding —class option to db:seed command:

You can checkout MySql database to verify whether record has inserted or not

Database seeding with Model Factories

Of course, manually specifying the records/attributes for each model seed is not good idea, instead of that we can use model factories to generate large amount of data.

Generate new Factory:

Generate new PostFactory class using command showing below:

The new PostFactory will be stored inside database/factories directory.

Go ahead and make change to the factory file as showing below:

Use Model Factory with Seeder Class:

Now instead of using direct value we will our model factory to generate multiple test records:

Open PostsTableSeeder.php file from seeds directory and make following changes:

Now you can execute db:seed command again to insert 50 users:

Checkout database to verify:

We are done!

let me know if you get any issues using this tutorial in comment section below. Thanks for reading.

Enjoy this tutorial? Get iTech Empires updates delivered straight to your inbox.

No Spam, ever. We'll never share your email address and you can opt out at any time.

Yogesh Kolihttp://www.itechempires.com
Yogesh Koli is a software engineer & Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 6+ years of experience working with the front-end and back-end web application development.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of

Tutorials You May Like!

- Advertisement -