giacomomasseron / laravel-models-generator
Generate Laravel models from an existing database
Fund package maintenance!
Giacomo Masseroni
Requires
- php: ^8.0.2||^8.2
- doctrine/dbal: ^3.7||^4.1
- illuminate/contracts: ^9.0||^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^6.1||^7.10.0||^8.1.1
- orchestra/testbench: ^10.2.1
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- pestphp/pest-plugin-type-coverage: ^3.2
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- rector/rector: ^2.0
README
Generate Laravel models from an existing database.
Compatible with Laravel 9/10/11/12.
Major features:
- PHPStan level 9/10 compliant
- Laravel 11 style
- Polymorphic relationships
- Enums casting
- Uuids / Ulids columns
Drivers supported
- MariaDB
- MySQL
- SQLite
- PostgreSQL
- SQLServer
Coming soon ... all drivers supported by doctrine/dbal.
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 [ 'clean_models_directory_before_generation' => true, 'generate_views' => false, /* |-------------------------------------------------------------------------- | Strict types |-------------------------------------------------------------------------- | | Add declare(strict_types=1); to the top of each generated model file | */ 'strict_types' => true, /* |-------------------------------------------------------------------------- | Models $table property |-------------------------------------------------------------------------- | | Add $table model property | */ 'table' => true, /* |-------------------------------------------------------------------------- | Models $connection property |-------------------------------------------------------------------------- | | Add $connection model property | */ 'connection' => true, /*'phpdocs' => [ 'scopes' => true, ],*/ /* |-------------------------------------------------------------------------- | Models $primaryKey property |-------------------------------------------------------------------------- | | Add $primaryKey model property | */ 'primary_key' => true, /* |-------------------------------------------------------------------------- | Primary Key in Fillable |-------------------------------------------------------------------------- | | Add primary key column field to fillable array | */ 'primary_key_in_fillable' => true, /* |-------------------------------------------------------------------------- | Timestamps customized fields |-------------------------------------------------------------------------- | | Change the default Laravel timestamps fields. | Ex. created_at => 'created_at', | updated_at => 'updated_at' | */ 'timestamps' => [ 'fields' => [ 'created_at' => null, 'updated_at' => null, ], 'format' => null, ], /* |-------------------------------------------------------------------------- | Models path |-------------------------------------------------------------------------- | | Where the models will be created | */ 'path' => app_path('Models'), /* |-------------------------------------------------------------------------- | Namespace |-------------------------------------------------------------------------- | | The namespace of the generated models | */ 'namespace' => 'App\Models', /* |-------------------------------------------------------------------------- | Parent |-------------------------------------------------------------------------- | | The parent class of the generated models | */ 'parent' => Illuminate\Database\Eloquent\Model::class, /* |-------------------------------------------------------------------------- | Base files |-------------------------------------------------------------------------- | | If you want to generate a base file for each model, you can enable this. | The base file will be created within 'Base' directory inside the models' directory. | If you want your base files be abstract you can enable it. | */ 'base_files' => [ 'enabled' => false, 'abstract' => true, ], /* |-------------------------------------------------------------------------- | Table prefix |-------------------------------------------------------------------------- | | Remove table prefix value from laravel model name | */ 'table_prefix' => '', /* |-------------------------------------------------------------------------- | Add comments in PHPDocs |-------------------------------------------------------------------------- | | Add comments to PHPDocs column property (Ex. @property int $id (comment)) | */ 'add_comments_in_phpdocs' => true, /* |-------------------------------------------------------------------------- | Relationships name case type |-------------------------------------------------------------------------- | | Define the way relation name are created. | Possible values: "camel_case", "snake_case" | */ 'relationships_name_case_type' => RelationshipsNameCaseTypeEnum::CAMEL_CASE, /* |-------------------------------------------------------------------------- | Polymorphic relationships |-------------------------------------------------------------------------- | | Define polymorphic relationships | | [ | 'table_name' => 'polymorphic_type', | | ex. for official laravel documentation | 'posts' => 'commentable', | ] | */ 'morphs' => [ ], /* |-------------------------------------------------------------------------- | Interfaces |-------------------------------------------------------------------------- | | Interface(s) implemented by all models | */ 'interfaces' => [ ], /* |-------------------------------------------------------------------------- | Traits |-------------------------------------------------------------------------- | | Trait(s) implemented by all models | */ 'traits' => [ ], /* |-------------------------------------------------------------------------- | Enums |-------------------------------------------------------------------------- | | Enum(s) implemented by all models | Ex. | 'column' => EnumClass::class, | */ 'enums_casting' => [ ], /* |-------------------------------------------------------------------------- | Uuids |-------------------------------------------------------------------------- | | If you want to use UUIDs in your models, you can define them here. | Ex. | 'table' => ['column1', 'column2'], | */ 'uuids' => [ ], /* |-------------------------------------------------------------------------- | Ulids |-------------------------------------------------------------------------- | | If you want to use Ulids in your models, you can define them here. | Ex. | ['table1', 'table2'], | */ 'ulids' => [ ], /* |-------------------------------------------------------------------------- | Excluded Tables |-------------------------------------------------------------------------- | | These models will not be generated | */ 'except' => [ 'migrations', 'failed_jobs', 'password_resets', 'personal_access_tokens', 'password_reset_tokens', ], /* |-------------------------------------------------------------------------- | Excluded Columns |-------------------------------------------------------------------------- | | These columns will not be added to $fillable array. | | You can use a string or any valid pattern for preg_match function. | Ex. '/your_pattern/' | '/your_pattern/i' (case-insensitive) | 'column_not_to_generate' | */ 'exclude_columns' => [ ], /* |-------------------------------------------------------------------------- | Excluded Relationships |-------------------------------------------------------------------------- | | These relationships will not be added to Model class. | Ex. | 'table_of_starting_relationship' => [ | 'table_of_relationship', | ], | */ 'exclude_relationships' => [ ], ];
Usage
php artisan laravel-models-generator:generate
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.