Use Laravel seed and sql files to populate database

后端 未结 4 1661
忘了有多久
忘了有多久 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:05

    As used by other answers, DB::unprepared does not work with more complex SQL files.

    Another better solution would be to use the MySQL cli directly inside a process:

    $process = new Process([
                'mysql',
                '-h',
                DB::getConfig('host'),
                '-u',
                DB::getConfig('user'),
                '-p' . DB::getConfig('password'),
                DB::getConfig('database'),
                '-e',
                "source path/to/schema.sql"
            ]);
            $process->mustRun();
    

提交回复
热议问题