Laravel – Seeders

Posted by itreeni-admin | September 12, 2017

Why Seeders are important

While a project is in development phase, developers have to drop and re-create all database tables in development life-cycle.

php artisan migrate:refresh

Laravel provides command line utility as mentioned above to drop and recreate tables but it removes stored data from tables. Inserting testing data manually is tedious, repetitive and time consuming.


How to create Seeder Classes

To automate this process, Laravel includes simple method of seeding database table using seeder class.

Laravel provides nice command  to generate Seeder class –

php artisan make:seeder Seeders/UsersTableSeeder

This will generate UsersTableSeeder file under Seeders folder. It contains only “run” method and being called when db:seed command is run from command prompt. We can write code to insert single/multiple data into DB table.


How to run Seeders

php artisan db:seed

This command is useful to run all seeders at one go. This is useful first time we create DB and wants to insert test data in multiple tables.

php artisan db:seed --class=UsersTableSeeder

Laravel also provides an option to run particular seeder. To do so, we need to provide seeder class name in “–class” option.

php artisan migrate:refresh --seed

Laravel also provide command to run all seeders after refreshing database. Rather than firing 2 separate commands we can use this one to fill db with test data immediately after refreshing db

Add a comment

*Please complete all fields correctly

Related Blogs

Posted by Rukmi Patel | October 20, 2020
Following a Unique & Streamlined Approach to Business – iTreeni Technolabs Thrives at GoodFirms
iTreeni Technolabs is a one-stop solution service provider incepted in 2017 and based in Waterloo, Australia. It possesses a stellar clientele experience with an adequate of repertory in accouching innovative...
Posted by Rukmi Patel | December 2, 2019
iTreeni Technolabs Outshines at GoodFirms by Providing End-To-End Business Solutions
Empowering clients' business by leveraging technology-enabled solutions endows iTreeni Technolabs to tap into the list of top web development companies in Australia at GoodFirms. View iTreeni Technolabs' GoodFirms' profile to...
MEAN-Vs-Full-stack
Posted by Rukmi Patel | April 9, 2019
Comparing Full Stack V/S MEAN Stack approach
The web and mobile app development services have stepped up to next level in last 5 years. Web and apps are all built utilizing a ‘stack’ of various technologies like...