47

Is there some way to generate models from database in Laravel?

The generators package only create an empty model.

Santi Barbat
  • 2,097
  • 2
  • 21
  • 26
  • 1
    What more are you expecting to be generated within the model file? – Bogdan May 31 '15 at 17:58
  • 1
    @Bogdan Because not all system use code first, sometimes need to use database first. That's crazy how biggest framework like Laravel doesn't support Database first officially. – Bcktr Nov 12 '21 at 20:56
  • @Bcktr while Laravel (or Django) is best suited for developing new applications, it’s quite possible to integrate it into legacy databases – Tiago Martins Peres Nov 13 '21 at 11:51

3 Answers3

80

Warning, you may end up overwriting your JetStream or any other scaffolding models. take backup of them before overwriting them.

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

coatesap
  • 10,707
  • 5
  • 25
  • 33
Cristian Llanos
  • 854
  • 1
  • 7
  • 10
  • The last command does no longer work with Laravel 5.6(.25): `There are no commands defined in the "code" namespace.` – Roland Jun 19 '18 at 13:41
  • 1
    Found it myself on documentation page: https://github.com/reliese/laravel/blob/master/README.md – Roland Jun 19 '18 at 13:50
  • Though this answer helped me generate model classes from my tables, the issue is that these classes do not include public properties for table columns. Thus I do not get any auto-completion benefits in the IDE (VSCode). – dotNET Aug 09 '18 at 01:49
  • Laravel (Eloquent) Models do not have public properties for table columns. These generated clases are Eloquent Models. In spirit of helping autocompletion I've added annotations to each generated class which some IDEs such as PhpStorm do use for autocompletion. – Cristian Llanos Aug 10 '18 at 02:17
  • most likely you _don't_ want to redo _all_ your models each time... what most people probably are looking for is to do a single table (for lets say, you just added the table `users` and need a model for it): `php artisan code:models --table=users` – Toskan Oct 05 '18 at 02:28
  • I have updated the reliese package to include PostgreSql... it can be found at https://github.com/rwdim/laravel . The original author is a genius, but he's not merging things with any speed at all. – Randy Dryburgh Jul 20 '19 at 21:09
  • You should use `php artisan config:clear` before `php artisan code:models`. It'll solve the problem of *ErrorException mkdir(): Invalid path* – Shrip Jun 13 '21 at 16:51
41

There is a library Eloquent Model Generator which aim is creating Eloquent models classes using database tables as a source. It generates not only class stub, but relation methods, docblocks for magic fields, additional properties etc.

It provides a console command php artisan krlove:generate:model ClassName for generating one class per one call, but you can create your own command to call this command for each table from your database.

CodeMonkey
  • 3,271
  • 25
  • 50
Andrii Mishchenko
  • 2,626
  • 21
  • 19
  • 1
    " but you can create your own command to call this command for each table from your database": https://github.com/krlove/eloquent-model-generator/issues/38#issuecomment-431660047 – jechaviz Jun 11 '21 at 18:12
7

Below package could be used for creating reverse models from database

composer require laracademy/generators

Please find the Documentation here -> https://github.com/laracademy/generators

Divyesh pal
  • 952
  • 1
  • 11
  • 17