I don’t understand how to add data to tables. I know I can use DB::insert to do it, but I also know that data should be added through seeders. I don’t want to call a seeder from a migration because if the seeder changes, it could break the migration. But I do understand that data should be added using seeders.
There’s another problem: if I run deployment through a script, I would have to modify the deployment script every time I need to add data to tables (if I call seeders manually).
Is there a simple way to add data just once?
2
Answers
Idk what do you mean, the seeders cannot broke migrations.
Use the seeders for add dummy demo data eg. demo users or products, or necessary default data like important attributes or values for products, location lists, etc…
I suggest to create a related model with the migration to use it in the seeders. It helps create and maintain the data.
The seeders are the silver bullet. If you write the seeders correctly, and run the migration with the seeders
php artisan migrate:fresh --seed
it will create a the database structure for you, and fill up with the necessary data you want. It is a fresh start.Of course if you change your db structure (add new tables or change tables structure by migrations), you probably need to recreate your related seeders as well.
Check the Laravel documentation:
https://laravel.com/docs/11.x/migrations
https://laravel.com/docs/11.x/seeding
I hope it helps
It may be difficult to select from migration or seeders to add data.
I suggest you create a separate directory for data migrations. This approach allows for a mixture of structured and structured data that distinguishes it from structural change. It also helps track the status of the one whose products have already been added, preventing duplication.
With data migration, you ensure a clear and manageable way to handle data entry, providing better content management and version control for your database
use AppServiceProvider or a Custom service provider to load the directory to the migration paths
this way you have control and track over what data you add