我从github获得了几个关于世界各国、各州和城市的.sql文件。如何使用Laravel的种子文件运行它们来填充数据库中的那些表?
发布于 2016-05-23 02:54:56
DB::unprepared()添加到DatabaseSeeder的run方法中。php artisan db:seed。
类DatabaseSeeder扩展Seeder { public function (){雄辩式:;$this->call('UserTableSeeder');$this->命令->info(‘User table seeded!');$path = 'app/developer_docs/countries.sql';DB::un准备(file_get_contents($path));$this->命令->info(’Country table种子!‘);}}发布于 2017-04-04 14:36:04
我找到了一个包,它可以从数据库表和行创建种子文件。它目前支持Laravel 4、5、6、7、8和9:
https://github.com/orangehill/iseed
最后,它基本上就像这样简单:
php artisan iseed my_table或多次:
php artisan iseed my_table,another_table发布于 2020-07-03 13:33:42
正如其他答案所使用的那样,DB::unprepared 不使用更复杂的文件。
另一个更好的解决方案是直接在进程中使用MySQL cli:
$process = new Process([
'mysql',
'-h',
DB::getConfig('host'),
'-u',
DB::getConfig('username'),
'-p' . DB::getConfig('password'),
DB::getConfig('database'),
'-e',
"source path/to/schema.sql"
]);
$process->mustRun();https://stackoverflow.com/questions/37369452
复制相似问题