22

I am new to laravel. 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.

Hitesh Modha
  • 2,740
  • 5
  • 28
  • 47

7 Answers7

19

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?

Ali Khalili
  • 1,504
  • 3
  • 18
  • 19
nielsstampe
  • 1,336
  • 1
  • 15
  • 25
  • 4
    Just to add, here is another package that I currently use: https://github.com/Xethron/migrations-generator. It is actively maintained and works great. – noeldiaz Jun 15 '14 at 01:44
  • in addition you can try https://packagist.org/packages/gguney/rmigration this package – Erkan Özkök Apr 05 '17 at 06:30
  • 2
    the JefferyWay generators do not generate a migration from existing database. How come this is the accepted answer for this question? – Mubashar Abbas Nov 08 '19 at 17:22
  • 3
    I just used https://github.com/kitloong/laravel-migrations-generator and it worked well for me. I discovered it by going to https://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][https://github.com/Xethron/migrations-generator/issues/200] regarding support for Laravel 7, a link was posted to try another one. – Tyler Collier Jul 30 '20 at 20:55
7

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

Jeremy Belolo
  • 4,319
  • 6
  • 44
  • 88
4

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.

Ernest Elikem
  • 316
  • 2
  • 12
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
Joel Lim
  • 57
  • 3
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

Akash khan
  • 861
  • 13
  • 25
  • 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. – speby May 11 '20 at 22:29
  • 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`. – Tyler Collier Jul 29 '20 at 20:39
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

mito
  • 61
  • 8
1

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

Henry
  • 7,721
  • 2
  • 38
  • 38