giacomomasseron/laravel-models-generator

Generate Laravel models from an existing database

v0.1.3 2024-11-01 14:33 UTC

This package is auto-updated.

Last update: 2024-11-01 14:34:03 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.