Categories: Mobile App

6 Tips About Data Seeding in Laravel – Laravel Daily

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.

vinova

Recent Posts

Guide to Facial Recognition Software Development in 2024

Facial recognition, once a staple of science fiction, is rapidly becoming a reality. In 2019,…

1 hour ago

Guide to Using AI in Recruitment Effectively in 2024

The recruitment picture is changing rapidly, and AI in recruitment is at the forefront of…

1 day ago

What is Multimodal AI? 10 Creative Applications and Real-World Examples

Multimodal AI is a groundbreaking technology that combines multiple modalities, such as text, images, and…

2 days ago

Top 10 AI Applications in the Energy Sector for 2024

Artificial intelligence (AI in the energy) sector is revolutionizing how we produce, distribute, and consume…

3 days ago

Top Mobile App Monetization Strategies for 2024

Nowadays, monetization application is the end game of mobile app development. Whether you're an indie…

4 days ago

Top Reasons Why Mobile Apps Fail and Lessons for Success

Nowadays, many mobile apps fail to make an impact. From poor research to bad user…

5 days ago