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.
Table of Contents
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.
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:
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.
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.
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.
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?
This content was originally published here.
Facial recognition, once a staple of science fiction, is rapidly becoming a reality. In 2019,…
The recruitment picture is changing rapidly, and AI in recruitment is at the forefront of…
Multimodal AI is a groundbreaking technology that combines multiple modalities, such as text, images, and…
Artificial intelligence (AI in the energy) sector is revolutionizing how we produce, distribute, and consume…
Nowadays, monetization application is the end game of mobile app development. Whether you're an indie…
Nowadays, many mobile apps fail to make an impact. From poor research to bad user…