Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rabol's avatar
Level 14

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

0 likes
18 replies
hero21's avatar

I think You should write it manually, There might not be something for changing database structure to migration file.

2 likes
londoh's avatar

@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.

1 like
moharrum's avatar

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

2 likes
rabol's avatar
Level 14

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!

cristianllanos's avatar

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:

  1. composer require reliese/laravel
  2. Add the service provider to your config/app.php file Reliese\Coders\CodersServiceProvider::class
  3. Publish the config file with php artisan vendor:publish --tag=reliese-models
  4. Make sure your database is correctly configured in config/database.php and .env files.
  5. 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 :)

11 likes
zeshan's avatar

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:

  1. Upload dump file (.sql) of your existing MySQL Database
  2. Select tables for which you want to generate Laravel Migrations
  3. Download Migrations

Live Instance: http://vizrex.com/tools/mysql-to-laravel

1 like
orenyny's avatar

Hi @rabol,

Did you use any of the methods offered? Which one did you find most helpful for you?

Thanks for sharing your experience :).

1 like
martinbean's avatar

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…

mselmany's avatar

@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.

4 likes
curtisbelt's avatar

@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 :)

Snapey's avatar

@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.

1 like
kevinbui's avatar

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.