[Laravel] Automatically generate your migrations from an existing database schema

Tram Ho

I. Introduction

When creating our migration . Sometimes it can be difficult to give an accurate way to add columns or fields to our table.

Sometimes, you have a database in one place, but you want to move this object to another server, or simply have a database design that you want to create a corresponding migration set. quickly. Wouldn’t it be great if we had a way to handle this for us automatically?

Fortunately, Laravel provides us with the “xethron / migrations-generator” package that solves this problem.

II. Setting

1. Install the package

Run command line with the command line below

For Laravel 4

For Laravel 5

2. Set the configuration in config / app.php file

For Laravel 4

For Laravel 5

III Configuration settings in the .evn file

As mentioned above, to create migration from an existing database we must specify the database with the tables we want to migrate.

Suppose we have a database named demo , then in .evn file where DB_DATABASE = homestead will be replaced with DB_DATABASE = demo

IV. How to use

To create migration for all tables in the database, run the following command

To create migration for the tables you want to create

Where table1, table2 are the tables that we want to convert from mysql database files to the corresponding migration You can also skip the tables that you do not want to create with the option –ignore = “table3, table4”

The Laravel Migrations Generator will first create all tables, columns and indexes, and then set all foreign key constraints.

V. Conclusion

As we can see, this is a really interesting Laravel Migration Generator builder that the community has created. It’s a great way to generate migration from an existing database and help us learn how to create migration .

Reference source Laravel Migration Generator

Share the news now

Source : Viblo