23

I know command to create database from laravel migration.

I have already created database in mysql. so how can i convert migration from that database?

7 Answers 7

18

You could use Jeffrey Way's generator tool for Laravel 4, and do them by hand. It is a very useful tool. https://github.com/JeffreyWay/Laravel-4-Generators (if you are using Laravel 5, use this package instead: https://github.com/laracasts/Laravel-5-Generators-Extended)

Or you could try out one of the following packages, they should convert your existing schema to Laravel migrations. I have not tried them, but have a look:

Also, read the following answer in another question, might be useful to you or others: Reverse engineering or Auto-Generations of Entities in Laravel?

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

4 Comments

Just to add, here is another package that I currently use: github.com/Xethron/migrations-generator. It is actively maintained and works great.
in addition you can try packagist.org/packages/gguney/rmigration this package
the JefferyWay generators do not generate a migration from existing database. How come this is the accepted answer for this question?
I just used github.com/kitloong/laravel-migrations-generator and it worked well for me. I discovered it by going to github.com/Xethron/migrations-generator, noting it said only up to Laravel 5.4 was supported, reading the Github issues saying there was no support for 5.8, and in [this issue][github.com/Xethron/migrations-generator/issues/200] regarding support for Laravel 7, a link was posted to try another one.
8

For modern versions of Laravel you should probably use a package like https://github.com/kitloong/laravel-migrations-generator

3 Comments

Do you know a package to create the seeders?
No sorry I didn't use any
I have just found orangehill/iseed ... for future reference ;) It says it supports Laravel 9 (i never lose faith)
6

If the accepted answer (at the time of editing this post) does not work for you, use this simple approach.

Check out this package by Kitloong is really easy to use.

https://github.com/kitloong/laravel-migrations-generator

After installation, just run something like this to export the migration to a location of your choice:

php artisan migrate:generate --path="path\to\existing\folder"

Example:

php artisan migrate:generate --path="C:\xampp\htdocs\laravel_bs4\database\migrations\my_new_migrations"

You should end up with migrations created from the database and saved in the location you have specified.

You can omit the --path option and simply run

php artisan migrate:generate 

This would export it to the default Laravel's migration folder (which is something I usually do not want).

Be sure to follow the prompts. Sticking to the default is usually okay.

Comments

3

I quick wrote this python script to make migration files out of an existing database.

https://github.com/aljorhythm/sql-to-laravel-migrations

After set up, just do

python retrieve-and-convert.py

1 Comment

This solution also provides a PHP script.
2

Simple solution, to use online Laravel migration generator for your existing SQL table schema without installing any package. Just enter your SQL schema and get Laravel migration file. Try: https://laravelarticle.com/laravel-migration-generator-online

2 Comments

This looks at first to be a useful tool. It immediately falls on its face using anything but the absolute most trivial and vanilla SQL. This is not a useful way to generate migrations for all but the absolutely most trivial case.
I'm not sure how old this tool is, but it is more trouble than it is worth. For the SQL create table definition of the field `status` int(1) NOT NULL DEFAULT 1, it was outputting a line like $table->integer('status',1)->default(1). That second parameter to integer() is meant to be the auto-increments boolean. So I kept getting errors like there can be only one auto column.
2

How about this?

Migrations Generator.It's for Laravel, 4, 5, 6, 7, but I don't know if it is for Laravel 8

2 Comments

(09-2020) fails on a fresh Laravel 8 install. Which is a pity.
Just added Laravel 8 support, apparently.
1

If you don't mind using Doctrine 2 in your project instead of Eloquent that has reverse engineering built in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.