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 [
'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' => [
],
];
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.