How to generate migration files from an existing database

widget

Overview

There are times when we will manually edit our table structure in our database for urgent reasons. We will then want to incorporate it into our migration files to migrate it later, while deploying on a live server.

To achieve this, we have to install a Laravel package called Laravel-migration-generator.

Step 1

Installation

Run the below command to install the package:

composer require --dev bennett-treptow/laravel-migration-generator

Step 2

Publish configuration

You also run the below command to publish the config files we will be using.

php artisan vendor:publish --provider="LaravelMigrationGenerator\LaravelMigrationGeneratorProvider"

Step 3

composer require --dev bennett-treptow/laravel-migration-generator

Step 4

Package registration

Add the following to your bootstrap/app.php file to register in the service provider.

$app->register(\LaravelMigrationGenerator\LaravelMigrationGeneratorProvider::class); 

Step 5

Usage

To generate the migration file of your database, you run the below command:

php artisan generate:migrations

By default, the file will go to tests/database/migrations to specify the path using the command:

php artisan generate:migrations --path=database/migrations

Where the database/migrations represents the path of your choice.

Free Resources