h8d8now h8d8now - 4 months ago 27
SQL Question

Laravel 5.2 - how to copy user data from external database with different table format

I have created a new project with laravel 5.2 which have

users
table containing
id, email, password
as provided in
database/migration/2014_10_12_000000_create_users_table
. I also added some fields such as fullname and address.

I also have old project with different table format and fields in its users table. Now, I need to copy / migrate all users information such as email, fullname, etc into my new project using laravel.

I know laravel provides seeding and multiple database connection. But I am not sure if it is ok to use seed with 10K records. Please advise the good way to copy those thousands records.

Thanks.

Answer

This is a good candidate for a Console Command.

php artisan make:console YourCommandName

public function handle()
{
    $mysql2 = DB::connection('mysql2')->select("var1 AS var2...")->get()->toArray();

    NewModel::insert($mysql2);
}

This is somewhat pseudo code. I haven't tested it, but that's how I would do it.

The other option is to create a new model in a foreach loop.

foreach ($mysql2 as $record)
{
    $model = new LaravelModel();
    $model->property = $record->property;
    $model->save();
}