14

我现在有我的数据库的新结构,但我需要以新格式导入旧数据。出于这个原因,我想使用 Laravel 播种机,但我需要以某种方式连接到旧数据库并进行选择查询并告诉播种机如何将数据放入新数据库中。

那可能吗 ?

4

3 回答 3

10

尝试:示例:

php artisan iseed my_table
php artisan iseed my_table,another_table

访问:https ://github.com/orangehill/iseed

于 2017-04-25T13:41:14.083 回答
6

配置您的 laravel 应用程序以使用两个 mysql 连接(如何在 Laravel 中使用多个数据库),一个用于新数据库,另一个用于旧数据库。

我会假装它像oldnew

在您的种子中从旧数据库读取并写入新数据库。

$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
         // ...
     ]);
}

确保在播种时添加消息,$this->command->info('Users table seeded');甚至是进度条(您可以访问命令行方法),以了解您在哪个导入点。

于 2016-09-21T22:40:27.483 回答
1


从Git repo下载包: https
://github.com/orangehill/iseed然后更新下面的文件 src/Orangehill/Iseed/IseedCommand.php 在第 75 行添加下面的代码

// update package script
    if($this->argument('tables') === null){
        $tables = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
    }

并使用以下代码更新同一文件中的 getArguments 方法

array('tables', InputArgument::OPTIONAL, 'comma separated string of table names'),

然后运行php artisan iseed,它将从现有数据库中获取所有表并开始为所有表创建播种器

于 2020-10-27T12:46:23.057 回答