What are Laravel seeders?
There are times when you create an application and it has configuration data that enables the application to run efficiently. This data will be placed as seeds in your application so as to enable its availability on installation.
Seeder
A seeder is a class used to insert data into the database by running a command.
Use of a seeder
Three main reasons why you would need a seeder are listed below.
-
# Configuration or Settings
Robust applications have configuration values that the application depends on and as such it is necessary to have a seeder in order to make deployment simple.
-
Default users
Because of security reasons some developers create their application admin account on the fly using seeders.
-
Demo data
To test an application we need demo data. On deployment, we can run our seeders so that we populate the application with dummy/demo data for test purposes.
Seeder commands
Writing seeders
php artisan make: seeder UserSeeder
The make:seeder command will generate a seeder. All seeders that will be generated will be placed in the database/seeders directory
Running seeders
php artisan db:seed
The db:seed Artisan command is used to seed the database. By default, this command runs the Database\Seeders\DatabaseSeeder class, which may, in turn, invoke other seed classes.
If a specific seeder class needs to be run individually, the --class option is used as shown below.
php artisan db:seed --class=UserSeeder