giacomomasseron / laravel-models-generator
Generate Laravel models from an existing database
Fund package maintenance!
Giacomo Masseroni
Requires
- php: ^8.2
- doctrine/dbal: ^4.1
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
Generate Laravel models from an existing database.
Installation
You can install the package via composer:
composer require giacomomasseron/laravel-models-generator
You can publish the config file with:
php artisan vendor:publish --tag="laravel-models-generator-config"
This is the contents of the published config file:
return [ 'table' => true, 'connection' => true, 'primary_key' => true, 'parent' => Illuminate\Database\Eloquent\Model::class, 'namespace' => 'App\Models', /** * [ * 'table_name' => 'polymorphic_type', * * ex. for official laravel documentation * 'posts' => 'commentable', * * ] */ 'morphs' => [ ], /** * Interface(s) implemented by all models */ 'interfaces' => [ ], /** * Trait(s) used by all models */ 'traits' => [ ], ];
Usage
php artisan laravel-models-generator:generate
Drivers supported
- MySQL
- SQLite
Coming soon ... all drivers supported by doctrine/dbal.
Polymorphic relationships
To add polymorphic relationships to your models, you can use morphs
array in the config file.
If you have tables like this:
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
And config file like this:
'morphs' => [ 'posts' => 'imageable' ],
This relationship will be created in the Image
model:
public function imageable(): MorphTo { return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id'); }
This relationship will be created in the Post
model:
public function images(): MorphMany { return $this->morphMany(Image::class, 'images'); }
Interfaces
If you want your models implement interface(s), use interfaces
value in config:
'interfaces' => [
],
Traits
If you want your models use trait(s), use traits
value in config:
'traits' => [
],
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.