> mobileapp Strategy - From Idea to Mobile App RealityVinova Our team will brainstorm with you on where to begin, where to go, and how to get you there. Whether you have a spark of an idea or an existing app – we can help. Getting your mobile strategy right is what our unique services are all about. We’ll wrestle with business challenges, discover new opportunities that will help you define and refine your product ideas into mobile app reality.

6 Tips About Data Seeding in Laravel – Laravel Daily

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.

Malcare WordPress Security

design firms in singapore,singapore web design services,graphic designer in singapore,singapore website design,mobile app development singapore,website design singapore,mobile application developer singapore,mobile application development singapore,ruby on rails developer singapore,app developer singapore,ios app development singapore,android developer singapore,mobile app developer singapore,mobile apps singapore,design agency singapore,web design singapore,mobile apps development singapore,developers in singapore,developer in singapore,web development singapore,website designer singapore,mobile game developer singapore,web design company singapore,singapore mobile application developer,web application singapore,ios developer singapore,mobile developer singapore,singapore app developer,app development singapore,singapore mobile app developer,web designer singapore,singapore web development,singapore web design,website developer singapore,web design services singapore,web development company singapore,website development singapore