Vince Carter Vince Carter - 29 days ago 8x
MySQL Question

Laravel seed database from existing database

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.

Is that possible ?


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.

I'll fake it like old and 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) {
         '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.