I think You should write it manually, There might not be something for changing database structure to migration file.
Create migrations and models from existing database
Hi Problem: I have an old 'plain' PHP website that I would like to migrate to Laravel So I would like to create migrations and Models for all the tables in the current database
Is there an easy way to create migrations and models from an existing database ?
Thanks in advance
If your database exists why bother with migrations?
Just create a model for each table and specify the table and primary key if they are not conventional. https://laravel.com/docs/5.2/eloquent#eloquent-model-conventions
You might want a migration if you want to include timestamps but thats optional
@rabol as Snapey said: if the tables exist you dont need a migration, and the model is pretty straight forward.
Unless perhaps you do need to re-create the db within laravel?
There are quite a few generator packages out there if you want something to scaffold the whole caboodle. I just now came across this one which seems to be "all singing and all dancing" and will scaffold from an existing db. I havent tried it tho:
http://labs.infyom.com/laravelgenerator/
regards
l.
For creating migrations from existing database tables I use laravel-migration-generator , models I create manually. If your tables have data you want to seed, I use:
exec("mysql -u ".\Config::get('database.mysql.user')." -p".\Config::get('database.mysql.password')." ".\Config::get('database.mysql.database')." < script.sql")
Which I found in this question
Hi
Thanks for all the suggestions.
It might not have been 100% clear, but as mentioned it is an old 'plain' PHP application that I would like to re-factor in Laravel, so some of the tables can be used as is, others needs to be re-factored, therefor I would like to create migrations for all the tables, manually re factor the tables that needs to be fixed, then create a new database with the refactored and do 'data migration'
I think I have a few things that I can start with, so thanks!
If you are using MySQL and Laravel 5.1 or above you can use php artisan code:models from this package reliese/laravel. All you need to do is:
-
composer require reliese/laravel - Add the service provider to your
config/app.phpfileReliese\Coders\CodersServiceProvider::class - Publish the config file with
php artisan vendor:publish --tag=reliese-models - Make sure your database is correctly configured in
config/database.phpand.envfiles. - And finally issue the command:
php artisan code:models
This package will scan your database and create all models for you. If you need something more specific, you can customize its config file.
Hope this helps :)
Hello @rabol,
We used this package to fulfill a similar requirement. It is much helpful but requires some additional work to generate migrations from existing database.
We've now made it lot more simpler, please have a look at this project on GitHub or visit the live instance at following link.
It's now as simple as 1-2-3:
- Upload dump file (.sql) of your existing MySQL Database
- Select tables for which you want to generate Laravel Migrations
- Download Migrations
Live Instance: http://vizrex.com/tools/mysql-to-laravel
I wrote this quick script to make migration files out of an existing database :) https://github.com/aljorhythm/sql-to-laravel-migrations
Hi @rabol,
Did you use any of the methods offered? Which one did you find most helpful for you?
Thanks for sharing your experience :).
Do you have a well structed database and you want to make a Laravel Application on top of it. Then this tool will help you a lot to generate laravel code.
@HERO21 - @hero21, We can generate migrations form Database tables using Xethron
Check this reference link: https://github.com/Xethron/migrations-generator
Simple solution! Laravel online migration generator. It really simple, just enter your existing SQL table schema and get Laravel migration file.
Try: https://laravelarticle.com/laravel-migration-generator-online
This one seems to be actively maintained:
This [package] seems to be actively maintained
@mselmany Unlike this thread.
The thread itself is nearly a decade old, and the last reply was almost half a decade ago…
@martinbean Apologies if I missed any guidelines, but is contributing to an older thread discouraged?
I realize that the original participants may have shifted focus to other topics or frameworks over the years. However, this thread continues to be among top search results, and updated information can benefit others who, like me, find it while searching.
Just to clarify, I have no affiliation with this tool; after evaluating several options, I found this one regularly updated and easy to use.
@mselmany I don't know the rules, but I think you did the right thing: I am one of those people searching for an answer to this, and am glad to scroll down and find more recent answers :)
@curtisbelt these days, you can squash migrations which takes an existing database and generates a sql script to create it. Any new migrations will run after this baseline.
Generating migration files from an existing database make total sense to me. I have done so once because I created a new project with an existing database. And I need the migration files to create an run PHPUnit test cases.
If anyone needs to do so in 2025, I have had very positive experiences with this package: https://github.com/bennett-treptow/laravel-migration-generator
Please or to participate in this conversation.