laravelmigrationlaravel-seeder

Laravel migration and seeders what I need to use and when to fill data?


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?


Solution

  • 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

    class AppServiceProvider extends ServiceProvider
    {
        public function register()
        {
            $this->loadMigrationsFrom([
                database_path('migrations'),
                database_path('data_migrations'),
            ]);
        }
    

    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