Create Migration Files from Existing Database in Laravel

In this article we will go over on how we can create migration files from existing database in Laravel, I am working on a project which was converted from legacy PHP code into Laravel and no migration files were created in the process of conversion.

You might be in similar situation that you have your database tables ready, and you are now looking to take advantage of Laravel Database Migration system.

Here is how you can generate migration files from Existing Database in two simple steps.

We will be using Xethron/migrations-generator package for this.

Step 1: Install the Migrations-Generator package

First step is to install the migrations generator package. Open your Terminal / Command Line and navigate to the project root directory.

Run the following command.

composer require --dev "xethron/migrations-generator"

This will install the package in your project and will install the required dependencies.

Composer require xethron/migration-generator

Once the package is installed , we can go ahead to generate migration files.

Step 2: Generate Migration Files

Using this package is smooth and requires a single command to execute to convert all your database tables into migration files.

On the project root in your terminal, execute this artisan command

php artisan migrate:generate

This will convert all your tables into migration files. If you are looking to generate files for specific tables, you can use this command

php artisan migrate:generate table1,table2,table3,table4,table5

You can also choose to ignore few tables and generate all others by using the following command

php artisan migrate:generate --ignore="table3,table4,table5"

That’s it ! The Migrations files will be created in your project in directory database > migrations .

Related Articles

8 comments On Create Migration Files from Existing Database in Laravel

  • Didn’t work with Laravel 7+

    Your requirements could not be resolved to an installable set of packages.

    Problem 1
    – xethron/migrations-generator v2.0.0 requires way/generators dev-feature/laravel-five-stable -> no matching package found.
    – Conclusion: remove laravel/framework v7.5.2
    – Conclusion: don’t install laravel/framework v7.5.2

  • So bad….

    You dont tell anything about register the providers…

    Source: https://github.com/Xethron/migrations-generator

    In older versions of the framework edit config/app.php and add this to providers section:

    Way\Generators\GeneratorsServiceProvider::class,
    Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class,
    If you want this lib only for dev, you can add the following code to your app/Providers/AppServiceProvider.php file, within the register() method:

    public function register()
    {
    if ($this->app->environment() !== ‘production’) {
    $this->app->register(\Way\Generators\GeneratorsServiceProvider::class);
    $this->app->register(\Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);
    }
    // …
    }

  • Thank you Tsugnani for this.

  • Hi , When I try to generate migration files from DB using this package , I got error like this.

    [Symfony\Component\Console\Exception\CommandNotFoundException]
    Command “migrate:generate” is not defined.
    Did you mean one of these?
    event:generate
    key:generate
    migrate:reset
    migrate:install
    migrate:refresh
    migrate:status
    migrate:rollback
    migrate

  • This command will create only schema migration, or also for existing data contained in the tables? Thank you.

  • This saved my life, thank you šŸ™‚

  • thank you , its so helpful.. this is exactly what i am looking for šŸ™‚

Leave a reply:

Your email address will not be published.

Site Footer