bennett-treptow/laravel-migration-generator

Create the `seed` from the data

julianovmartins opened this issue · 2 comments

Suggestion

This package for Laravel does an exceptional job and has saved a lot of development time on creating the new migrations from the existing tables.

A feature that would be nice to have implemented in this package is creating the seed from the data.

Command example:

php artisan generate:migrations --seed=model --transaction
php artisan generate:migrations --seed=table // no transaction

Seeder example:

class UserSeeder extends Seeder
{
    public function run()
    {
        // With or without closure transaction
        DB::transaction(function () {

            // Multiple insertions
            DB::table('users')->insert(['name1..' => 'Name', 'email' => 'email1...', 'password' => 'hash1...']);
            DB::table('users')->insert(['name2..' => 'Name', 'email' => 'email2...', 'password' => 'hash2...']);
            DB::table('users')->insert(['name3..' => 'Name', 'email' => 'email3...', 'password' => 'hash3...']);

            // Or multiple creations
            User::create(['name1..' => 'Name', 'email' => 'email1...', 'password' => 'hash1...']);
            User::create(['name2..' => 'Name', 'email' => 'email2...', 'password' => 'hash3...']);
            User::create(['name3..' => 'Name', 'email' => 'email3...', 'password' => 'hash3...']);
        });


        // or data in array
        $data = [
            ['name1..' => 'Name', 'email' => 'email1...', 'password' => 'hash1...'],
            ['name2..' => 'Name', 'email' => 'email2...', 'password' => 'hash2...'],
            ['name3..' => 'Name', 'email' => 'email3...', 'password' => 'hash3...'],
        ];

        // Transaction in another way
        try {
            DB::beginTransaction();
            foreach ($data as $item) {
                // table
                DB::table('users')->insert($item);
                // model
                User::create($item);
            }
            DB::commit();
        } catch (Throwable $e) {
            DB::rollback();
        }
    }
}

There is a package that does this, but it is out of date.
https://github.com/orangehill/iseed

I think this is out of scope for this package. Seeds are not migrations, and this is just a migration generator.

You should be able to use any number of ways to export data to a seeder: I know DataGrip (JetBrains IDE) has a way to export rows as a csv or as JSON which could be copy/pasted into a seeder.

Looks like another package like https://github.com/joeyrush/better-migrate-seed would do!

I would possibly be interested in making a separate package for this, but it would not be part of this package.

Thanks for the answer! 😉