Generate Laravel models from an existing database.
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,
/**
* Add declare(strict_types=1); to the top of each generated model file
*/
'strict_types' => true,
/**
* Add $connection model property
*/
'connection' => true,
/**
* Add $table model property
*/
'table' => true,
/**
* Add $primaryKey model property
*/
'primary_key' => true,
/**
* Add $primaryKey field to fillable array
*/
'primary_key_in_fillable' => 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' => [
],
/**
* Enum(s) used in laravel casts function
*/
'enums_casting' => [
],
/**
* Excluded Tables
*/
'except' => [
'migrations',
'failed_jobs',
'password_resets',
'personal_access_tokens',
'password_reset_tokens',
],
];
php artisan laravel-models-generator:generate
- MySQL
- SQLite
Coming soon ... all drivers supported by doctrine/dbal.
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');
}
If you want your models implement interface(s), use interfaces
value in config:
'interfaces' => [
],
If you want your models use trait(s), use traits
value in config:
'traits' => [
],
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.