Table of Contents
Laravel has this amazing feature of seeding databases with test data using seed classes. These classes are stored in database/seeds
.
Here in this document, we will discuss how to seed data effectively.
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);
}
}
The above code is a bad practice in the 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 the future. To solve 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()
it by clicking here.
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 a 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 the required one.
php artisan db:seed --class=UsersTableSeeder
The above code only seeds data in the User table.
Execute Seeder Class From Migration Files
Generally, we create a table and seed data into that table immediately. This process can’t applied in the production stage. If you have an automated deployment setup that involves only artisan migration then it becomes even more complicated.
We can resolve this problem by launching a 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
]);
}
DataSeeding Method for Local and Production
In some cases, users may want to seed data in only the 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);
}
}
}
Generate a Database-based fill class using iSeed
iSeed is a tool that is used for this purpose. You can get iSeed Generator from Git Hub. After installing the tool, run the following command:
$ php artisan iseed users --force
The above code generates a 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.