18

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 ?

2
  • I think your best bet is to export your data from the old database with a tool such as phpMyAdmin, modify the data to fit your new database and then upload it. Commented Sep 21, 2016 at 21:26
  • by modifying do u mean in editor ? Commented Sep 21, 2016 at 21:42

3 Answers 3

15

Try: Examples:

php artisan iseed my_table
php artisan iseed my_table,another_table

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

Sign up to request clarification or add additional context in comments.

1 Comment

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
8

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.

2 Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.