Laravel 5.5 Seeding Example | Laravel Seeding | Laravel Database: Seeding

Seeding is very useful while you wanted to keep some dummy data without manual interfere with Database. For example when you deploy you application you insert admin credentials into you users table every time you deploy so that admin user can login. You don't need to to insert those credentials manually every time.
Laravel provides feature of seeding and you can manage your dummy database in your seed classes.



You can create seed like below artisan command:

These command will create a new file 'database/seeds/ProductsTableSeeder.php' and 'database/seeds/UsersTableSeeder.php' which will contain a run() method.

You just need to write insert query inside that run() method like below:

UsersTableSeeder.php



ProductsTableSeeder.php

Here I'm inserting a list of products into products table.
Now you will require to call this 'ProductsTableSeeder' and 'UsersTableSeeder' class in 'DatabaseSeeder.php' like below code:

You are all done with creating seeder.
Now you can run your seeder with below command:

This above command will run your every seeder. You also can manually run a particular seeder like below command:

You also can migrate and seed the data in one command like:

Drop All Tables & Migrate

The migrate:fresh command will drop all tables from the database and then execute the migrate command:

The below command will drop all tables from the database and then execute the migrate command after that will run all the seeders.

Laravel Migration Example | Database: Migrations - Laravel | Laravel 5.5 Migration Tutorial | Database Versioning

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *