PHPackages                             giacomomasseron/laravel-models-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. giacomomasseron/laravel-models-generator

ActiveLibrary[Database &amp; ORM](/categories/database)

giacomomasseron/laravel-models-generator
========================================

Generate Laravel models from an existing database

v1.3.2(1mo ago)484.2k—10%2MITPHPPHP ^8.0.2||^8.2||^8.3CI passing

Since Oct 12Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/giacomomasseron/laravel-models-generator)[ Packagist](https://packagist.org/packages/giacomomasseron/laravel-models-generator)[ Docs](https://github.com/giacomomasseron/laravel-models-generator)[ GitHub Sponsors]()[ RSS](/packages/giacomomasseron-laravel-models-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (32)Versions (46)Used By (0)

Generate Laravel models from an existing database
=================================================

[](#generate-laravel-models-from-an-existing-database)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d81d324645fd709da586e68b9f95596dedca7fe1a23810cbafd8decc35fea387/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f676961636f6d6f6d61737365726f6e2f6c61726176656c2d6d6f64656c732d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/giacomomasseron/laravel-models-generator)[![GitHub Tests Action Status](https://camo.githubusercontent.com/0306a2ee2f24750007071298e9732f1e04747f2ae4d983a121c6d66e6defbe7d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f676961636f6d6f6d61737365726f6e2f6c61726176656c2d6d6f64656c732d67656e657261746f722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/giacomomasseron/laravel-models-generator/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/f70fd09e65acd4fd28372d70c8b85bc8481583146ec668dadd5f9ea1617dd6b2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f676961636f6d6f6d61737365726f6e2f6c61726176656c2d6d6f64656c732d67656e657261746f722f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/giacomomasseron/laravel-models-generator/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/727208fa4dfb106043acabcab037f04613005af2932aa2abf4e4b3c70887032d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f676961636f6d6f6d61737365726f6e2f6c61726176656c2d6d6f64656c732d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/giacomomasseron/laravel-models-generator)

Compatible with Laravel 9/10/11/12/13.

Major features:

- PHPStan level 9/10 compliant
- Laravel 11/12/13 style (using new models attributes)
- Polymorphic relationships
- Uuids / Ulids columns
- Enums casting
- URI casting
- JSON casting

Drivers supported
-----------------

[](#drivers-supported)

- MariaDB
- MySQL
- SQLite
- PostgreSQL
- SQLServer

Coming soon ... all drivers supported by doctrine/dbal.

Factories
---------

[](#factories)

The package can generate factories for your models.
The factories will be created in `database/factories` directory, and they use standard Laravel `fake` functions to fill fields.

You can generate model factories with the command:

```
php artisan laravel-models-generator:generate -f
```

or

```
php artisan laravel-models-generator:generate --factories
```

Other usages
------------

[](#other-usages)

To generate models from a specific connection:

```
php artisan laravel-models-generator:generate -c=connection
```

To generate models from a specific schema:

```
php artisan laravel-models-generator:generate -s=schema
```

To generate a single table:

```
php artisan laravel-models-generator:generate -t=users
```

Installation
------------

[](#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="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,
        'generate_children_classes' => 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' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | Table Traits
    |--------------------------------------------------------------------------
    |
    | Trait(s) implemented by specific models.
    | Ex.
    |   'table' => TraitClass::class|[TraitClass::class],
    |
    */
    'table_traits' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | Enums
    |--------------------------------------------------------------------------
    |
    | Enum(s) implemented by all models
    | Ex.
    |   'column' => EnumClass::class,
    |
    */
    'enums_casting' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | URI casting
    |--------------------------------------------------------------------------
    |
    | Columns that has to be cast as URI.
    | Ex.
    |   'table1' => [
    |       'column1',
    |       'column2',
    |   ],
    |   'table2' => [
    |       'column3',
    |       'column4',
    |   ],
    |
    */
    'uri_casting' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | JSON casting
    |--------------------------------------------------------------------------
    |
    | Columns that has to be cast as JSON.
    | Ex.
    |   'table1' => [
    |       'json_column1' => ['json_key1', 'json_key2'],
    |       'json_column2' => ['json_keyA', 'json_keyB'],
    |   ],
    |   'table2' => [
    |       'json_column3' => ['json_keyX', 'json_keyY'],
    |       'json_column4' => ['json_keyM', 'json_keyN'],
    |   ],
    |
    */
    'json_casting' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | Global scopes
    |--------------------------------------------------------------------------
    |
    | Global scope(s) applied to specific models.
    | Ex.
    |   'table1' => [
    |       GlobalScopeClass::class',
    |   ],
    |   'table2' => [
    |       GlobalScopeClass1::class,
    |       GlobalScopeClass2::class,
    |   ],
    |
    */
    'global_scopes' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | 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' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | Observers
    |--------------------------------------------------------------------------
    |
    | Observer(s) implemented by specific models.
    | Ex.
    |   'table' => ObserverClass::class,
    |
    */
    'observers' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | Query Builders (Laravel 12.19+)
    |--------------------------------------------------------------------------
    |
    | Query builder(s) implemented by specific models.
    | Ex.
    |   'table' => QueryBuilder::class,
    |
    */
    'query_builders' => [
    ],

    /*
    |--------------------------------------------------------------------------
    | 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 Factories
    |--------------------------------------------------------------------------
    |
    | These factories will not be generated.
    |
    */
    'exclude_factories' => [
        'users',
        'jobs',
        'job_batches',
        'failed_jobs',
    ],

    /*
    |--------------------------------------------------------------------------
    | Excluded Relationships
    |--------------------------------------------------------------------------
    |
    | These relationships will not be added to Model class.
    | Ex.
    |   'table_of_starting_relationship' => [
    |       'table_of_relationship',
    |   ],
    |
    */
    'exclude_relationships' => [
    ],
];
```

Usage
-----

[](#usage)

```
php artisan laravel-models-generator:generate
```

Polymorphic relationships
-------------------------

[](#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
----------

[](#interfaces)

If you want your models implement interface(s), use `interfaces` value in config:

```
'interfaces' => [
],
```

Traits
------

[](#traits)

If you want your models use trait(s), use `traits` value in config:

```
'traits' => [
],
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Giacomo Masseroni](https://github.com/giacomomasseron)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance89

Actively maintained with recent releases

Popularity35

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 90.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~12 days

Recently: every ~43 days

Total

45

Last Release

58d ago

Major Versions

v0.4.5 → v1.0.02025-05-24

PHP version history (3 changes)v0.0.1PHP ^8.2

v0.3.5PHP ^8.0.2||^8.2

v1.3.1PHP ^8.0.2||^8.2||^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/504902576948b6d89fe41cd186b483afa89cc9de1b238005a5b7dcc51d33997e?d=identicon)[giacomomasseron](/maintainers/giacomomasseron)

---

Top Contributors

[![giacomomasseron](https://avatars.githubusercontent.com/u/16156317?v=4)](https://github.com/giacomomasseron "giacomomasseron (194 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (15 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")

---

Tags

databaseeloquenteloquent-modelslaravellaravel-componentsmysql-databasephpscaffoldlaraveleloquentGiacomo Masseronilaravel-models-generator

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/giacomomasseron-laravel-models-generator/health.svg)

```
[![Health](https://phpackages.com/badges/giacomomasseron-laravel-models-generator/health.svg)](https://phpackages.com/packages/giacomomasseron-laravel-models-generator)
```

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spatie/laravel-model-flags

Add flags to Eloquent models

4301.1M1](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17649.9k](/packages/lacodix-laravel-model-filter)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[indexzer0/eloquent-filtering

Powerful eloquent filtering

22425.9k3](/packages/indexzer0-eloquent-filtering)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
