16

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 ?

Vince Carter
  • 907
  • 3
  • 14
  • 41

3 Answers3

12

Try: Examples:

php artisan iseed my_table
php artisan iseed my_table,another_table

Visit: https://github.com/orangehill/iseed

Floern
  • 33,559
  • 24
  • 104
  • 119
  • the problem with this package is the tables of whom we want to create a seeder we have to tell about it In my case I want to create a seeder for all the tables and this package fails to achieve that goal. If I'm missing something please advice – Gardezi Dec 29 '17 at 12:55
7

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) {
     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.

Community
  • 1
  • 1
phaberest
  • 3,140
  • 3
  • 32
  • 40
  • Thank you @phaberest, unfortunately i miss something. I have made a second connection (mysql_old) as in the example and put the name of it in the new DB:connection('mysql_old'). With all() it gives me Call to undefined method Query\Builder::all(). I have tried with select() and i put a print_r($old_users); and here comes the strange part. It prints me the whole MysqlConnection object, but in the config part the name is 'mysql_old', but the database is the wrong one from 'mysql' connection. – Vince Carter Sep 22 '16 at 05:59
  • Make sure you don't reference any `.env` variable in your `config/database.php` as they have precedence over any other setting. You caught me...it has to be `get()` instead of `all()` :D – phaberest Sep 22 '16 at 08:43
1

Download package from
Git repo : https://github.com/orangehill/iseed
then update below file src/Orangehill/Iseed/IseedCommand.php Add below code at line number 75

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

and update getArguments method in same file with below code

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

and then run php artisan iseed so it will get all the tables from your existing db and start creating seeders for all tables