Laravel has this amazing feature of seeding database with test data using seed classes. These classes are stored in database/seeds
.
Here in this document, we will discuss about how to seed data effectively.
1. Avoid seed duplication using updateOrCreate()
It is uncommon for data seeding code to run more than once but ignorance is not an option. Below is an example of bad practice:
public function run()
{
$items = [
['id' => 1, 'title' => 'Administrator'],
['id' => 2, 'title' => 'Simple user'],
];
foreach ($items as $item) {
Role::create($item);
}
}
Above code is a bad practice in a sense that if we try to run this code twice it will fail because of duplicate IDs. Some beginners may think that removing ID will solve that problem but rather than solving it will create much bigger problems in future. For solving these problems, updateOrCreate()
is the best option:
foreach ($items as $item) {
Role::updateOrCreate(['id' => $item['id']], $item);
}
If we run the above code twice, it updates existing data if it exists and also creates new data. You can get more details about updateOrCreate()
by clicking here.
2. Run only one Seeder Class
Seeder consists of multiple seeder classes hence most of the people run the whole seeder class. Laravel also provides this amazing feature of running specific seeder class.
php artisan db:seed
Many people run the above code which imports all data. But one can also restrict data to only required one.
php artisan db:seed --class=UsersTableSeeder
The above code only seeds data in User table.
3. Execute Seeder Class From Migration Files
Generally, we create table and seed data into that table immediately. This process cant applied in production stage. If you have automated deployment setup which involves only artisan migrate then it becomes even more complicated.
We can resolve this problem by launching specific seeder from migration directly:
public function up()
{
Schema::create('themes', function (Blueprint $table) {
$table->increments('id');
$table->text('name');
});
Artisan::call('db:seed', [
'--class' => ThemesTableSeeder::class
]);
}
4. DataSeeding Method for Local and Production
In some cases, users may want to seed data in only local environment but not in production for which we can use the code below. It is not the best method but it works fine.
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
if (app()->environment() == 'production') {
$this->call(ThemesTableSeeder::class);
$this->call(LanguagesTableSeeder::class);
} else {
$this->call(UsersTableSeeder::class);
$this->call(ModulesTableSeeder::class);
$this->call(ThemesTableSeeder::class);
$this->call(LanguagesTableSeeder::class);
}
}
}
5. Generate a Database-based fill class using iSeed
iSeed is a tool that is used for this purpose. You can get iSeed Generator from github. After installing the tool, run following command:
$ php artisan iseed users --force
The above code generates UserTableSeeder file. --force
is for overwriting of any existing fill class.
These are some methods that i have been using. Be sure to check for more ways out there. Also, check out more tutorials by clicking here.