Skip to content

giacomomasseron/laravel-models-generator

Repository files navigation

Generate Laravel models from an existing database

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

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages