Use Laravel seed and sql files to populate database

后端 未结 4 1693
忘了有多久
忘了有多久 2021-01-30 11:28

I got several .sql files of countries, states and cities of the world from github. How can I run them with Laravel\'s seed files to populate those tables in my database?

4条回答
  •  难免孤独
    2021-01-30 12:04

    1. Add DB::unprepared() to the run method of DatabaseSeeder.
    2. Run php artisan db:seed at the command line.

      class DatabaseSeeder extends Seeder {
      
          public function run()
          {
              Eloquent::unguard();
      
              $this->call('UserTableSeeder');
              $this->command->info('User table seeded!');
      
              $path = 'app/developer_docs/countries.sql';
              DB::unprepared(file_get_contents($path));
              $this->command->info('Country table seeded!');
          }
      }
      

提交回复
热议问题