Contact Us

6 Tips About Data Seeding in Laravel – Laravel Daily

Mobile App | June 28, 2021

Laravel migration mechanism has a great function of seeding data. In this article, I will show random tips from my own experience, how to use seeding in real-life cases.

Tip 1. Use updateOrCreate() to avoid double-seeding

Imagine this seeder code, and imagine if for some reason this seeder would be launched more than once:

Second attempt to run seeder would probably fail because of conflicting IDs. In other scenario, if you don’t specify IDs, then you may end up with too much data in the table, with repeating entries. To avoid that, do this:

Some more details in my other article.

Tip 2. Run only one Seeder class

Some time ago I was quite surprised by how many people don’t know that you can specify a seeder class when running php artisan db:seed.

This command above will launch everything listed in DatabaseSeeder.php file.

But you can limit the launch to one exact seeder:

Tip 3. Run Seeder Class From Migration

Quite often you need to create a new DB table and immediately seed it with some data. But in production environment you can’t just run “artisan db:seed”, especially if you have automated deployment setup which involves only “artisan migrate” command.

The trick is to launch a specific seeder from migration file itself.

Tip 4. Seeder Factory with Relationship: Use Parent’s Factory

If you use Factories for your seeds, how do you set up relationships between two models? For example, you need to seed 10 companies and 10 contacts within those companies?

Here’s how database/factories/CompanyFactory.php may look like:

See how company_id field is filled in? With another factory.

There’s also another, perhaps less popular way – read about it in my another article.

Tip 5. DatabaseSeeder for Local and Production

Sometimes you need to seed some data only in your local environment, but not in production. Or, use different seeder files for different environments.

Not sure if it’s the most elegant way, but here’s how in the past I’ve achieved different seeding for local and production environments.

Tip 6. Use iSeed to Generate Seeders from Database

Final tip is actually a tool I’ve used myself quite a few time, it’s called iSeed Generator.

Here’s my video about it:

That’s it. Final “general” tip would be to form your seeders in a way that on your local/staging environments you could run “artisan migrate:fresh –seed” safely multiple times, without losing any important data, but on production environment you need to run “artisan db:seed” only once, and then forget about seeders. If you need to run some seed on production, put it in migration, see Tip 3.

Would you add any more tips about seeding in Laravel?

Like our articles?
Check out our Laravel online courses!

This content was originally published here.