Create Models from database in Laravel

83,375

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
83,375
Santi Barbat
Author by

Santi Barbat

Updated on July 09, 2022

Comments

  • Santi Barbat
    Santi Barbat almost 2 years

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

    The generators package only create an empty model.