来自现有数据库的 Laravel 种子数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39626732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel seed database from existing database
提问by Vince Carter
i have now new structure of my database, but i need to import the old data in the new format. For that reason i want to use the Laravel seeder, but i need somehow to connect to the old database and make select queries and to tell the seeder how to put the data in the new database.
我现在有我的数据库的新结构,但我需要以新格式导入旧数据。出于这个原因,我想使用 Laravel 播种机,但我需要以某种方式连接到旧数据库并进行选择查询,并告诉播种机如何将数据放入新数据库中。
Is that possible ?
那可能吗 ?
采纳答案by phaberest
Configure your laravel app to use two mysql connections (How to use multiple database in Laravel), one for the new database, the other for the old one.
将您的 Laravel 应用程序配置为使用两个 mysql 连接(如何在 Laravel 中使用多个数据库),一个用于新数据库,另一个用于旧数据库。
I'll fake it like old
and new
.
我会像old
和一样伪造它new
。
In your seeds read from the old database and write into the new.
在您的种子中从旧数据库读取并写入新数据库。
$old_user = DB::connection('old')->table('users')->get();
foreach ($old_users as $user) {
DB::connection('new')->table('users')->insert([
'name' => $user->name,
'email' => $user->email,
'password' => $user->password,
'old_id' -> $user->id
// ...
]);
}
Make sure to add messages while seeding like $this->command->info('Users table seeded');
or even a progress bar (you can access command line methods) to know at which point of the import you are.
确保在播种时添加消息,$this->command->info('Users table seeded');
甚至进度条(您可以访问命令行方法)以了解您在导入的哪个点。
回答by Agidigbi Ayodeji Victor
Try: Examples:
尝试: 示例:
php artisan iseed my_table
php artisan iseed my_table,another_table