filippo-toso / models-generator
A Laravel Artisan command to automatically generate models from database tables
Installs: 1 035
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.0 || ^8.0 || ^9.0
- doctrine/dbal: ^2.8 || ^3.0
- illuminate/support: >=8.0
- laravel/helpers: ^1.3
- dev-master
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.3.0
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.6.1
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
This package is auto-updated.
Last update: 2024-11-26 06:49:27 UTC
README
A Laravel Artisan command to automatically generate models from database tables.
Requirements
- PHP 5.6+
- Laravel 5.4+
- Doctrine DBAL +2.8
Installing
Use Composer to install it:
composer require filippo-toso/models-generator
How does it work?
This generator is very simple. It builds the models from the database and saves them in the App\Models namespace. Then extends these models with the user's counterpart in the App namespace. If you follow Laravel's guidelines for tables and columns naming, it works like a charm ;)
After you have executed the first generation, you can go and customize the models in the App namespace as usual. If you change the database (as often happens during development), you can run the generator again (with the override option enabled) and get an updated set of models without any additional effort.
This solution leaves you the benefit of automatic code generation plus the ability to add/change the behavior of your models (ie. change attributes visibility, add relationships, and so on).
By default the generator doesn't create the models of Laravel's tables like jobs, cache, and so on. You can modify this behavior publishing the package resources and editing the config/models-generator.php file.
Configuration
You can publish the configuration file with the following command:
php artisan vendor:publish --tag=config --provider="FilippoToso\ModelsGenerator\ServiceProvider"
The config/model-generator.php file allows you to:
- define which tables exclude form the generation (ie. cache, jobs, migrations, ...)
- define one to one relationships
- define polymorphic relationships
Just open the file and read the comments :)
Keep in mind that the one to many and many to many relationships are built using the foreign keys you define in the database.
Options
The predefined use from command line is:
php artisan generate:models
This command executes the following steps:
- Creates an App\Models namespace.
- Fills it with the Models from the database.
- Creates the user editable models in the App namespace.
- Creates the App\Models\Traits\UserRelationships trait for the User model.
If there are existing models in the App\Models namespace they will not be overwritten by default (see the next example). If there are existing models in the App namespace they will never be overwritten.
You can modify the default behavior using the following parameters:
php artisan generate:models --overwrite
With the overwrite option set the generator will always overwrite the models in the App\Models namespace. This can be done safely if you follow the rule to not change these models but edit the ones in the App namespace.
php artisan generate:models --connection=sqlite
You can specify a different connection if you need to.
Workflow
To gain the maximum benefits from this package you should follow this workflow:
- design the database
- write the migrations (including all the required foreign keys)
- migrate the database
- configure the generator
- run the generator
- customize the models in the App namespace
Then, every time you create and run a new migration, you should execute the generator again to keep the models in sync with the database.
You must also follow Laravel's guidelines about tables and columns names otherwise the generator will not be able to identify the existing relationships.