4

I found many results how to create migration from a model, but is there any way to create a model from migrations?

Basically I want to give a table name, and if there is multiple migration files (one to create and others to update the previous states) the model could be updated.

Is there any solution for this?

netdjw
  • 5,419
  • 21
  • 88
  • 162
  • What do you want to be updated in the model? – Rwd Jul 20 '19 at 09:17
  • I create a migration what define `name` and `size` fileds. Later I create an other migration what is extend this table with a `color` filed. Now this is 2 files in migrations. If I generate model only from the first one it will be not good. – netdjw Jul 20 '19 at 09:38
  • ...but you would only want one `Model` as there is only one table. So, what would you want to change in that model class based on the 2nd migration? – Rwd Jul 20 '19 at 09:41
  • Okay, in my head was the migration files, not the tables. Now I understand you. – netdjw Jul 20 '19 at 09:43

1 Answers1

4

I'm not sure that there's a good way to create a model directly from migrations, as it depends on what database you use as to the exact structure of your tables.

There seems to be a package which support generating the models from your database, however: https://github.com/laracademy/generators

atymic
  • 3,093
  • 1
  • 13
  • 26
  • I using many migrations with many fields, and if the generated models aren't perfect is better than write each by hand. – netdjw Jul 20 '19 at 09:36
  • 2
    The package above should work correctly, provided your database is migrated when you run the generation command. – atymic Jul 20 '19 at 09:37
  • Warning: the package above laraacademy/generators did NOT work well for me. It created the wrong fields. fyi – WebDev-SysAdmin Aug 10 '22 at 14:49