Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Laravel seed and sql files to populate database

Tags:

laravel-4

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?

like image 458
Andrew Koper Avatar asked May 22 '16 00:05

Andrew Koper


People also ask

What is database seeding in laravel?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.


1 Answers

  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!');     } } 
like image 135
Andrew Koper Avatar answered Oct 22 '22 04:10

Andrew Koper