PHPackages                             jonathanguo/eloquent-model-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. jonathanguo/eloquent-model-generator

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

jonathanguo/eloquent-model-generator
====================================

Eloquent Model Generator

2.0.15(4y ago)629.2k↓45%2[1 PRs](https://github.com/JonathanGuo/eloquent-model-generator/pulls)MITPHP

Since Jul 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/JonathanGuo/eloquent-model-generator)[ Packagist](https://packagist.org/packages/jonathanguo/eloquent-model-generator)[ RSS](/packages/jonathanguo-eloquent-model-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (21)Used By (0)

Eloquent Model Generator
========================

[](#eloquent-model-generator)

Eloquent Model Generator is a tool based on [Code Generator](https://github.com/JonathanGuo/code-generator) for generating Eloquent models.

- Support Laravel 5, 6, 7 and 8.
- Use [barryvdh/laravel-ide-helper](https://github.com/barryvdh/laravel-ide-helper) to generate better DocBlock into models, which is more dev-friendly.
- Auto generate `casts` and `softDeletes` trait.

Why I created another generator?
--------------------------------

[](#why-i-created-another-generator)

This package is based on [krlove/code-generator](https://github.com/krlove/eloquent-model-generator). The maintainer looks like not responding to GitHub any longer. So I mirrored the repo and do some customisation to meet my needs.

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

[](#installation)

Step 1. Add Eloquent Model Generator to your project:

```
composer require jonathanguo/eloquent-model-generator --dev

```

Step 2. Register `GeneratorServiceProvider`:

```
'providers' => [
    // ...
    JonathanGuo\EloquentModelGenerator\Provider\GeneratorServiceProvider::class,
];
```

If you are using Laravel version 5.5 or higher this step can be omitted since this project supports [Package Discovery](https://laravel.com/docs/5.5/packages#package-discovery) feature.

Step 3. Configure your database connection.

Usage
-----

[](#usage)

Use

```
php artisan generate:model User

```

to generate a model class. Generator will look for table with name `users` and generate a model for it.

### table-name

[](#table-name)

Use `table-name` option to specify another table name:

```
php artisan generate:model User --table-name=user

```

In this case generated model will contain `protected $table = 'user'` property.

### output-path

[](#output-path)

Generated file will be saved into `app/Models` directory of your application and have `App\Models` namespace by default. If you want to change the destination and namespace, supply the `output-path` and `namespace` options respectively:

```
php artisan generate:model User --output-path=/full/path/to/output/directory --namespace=Some\\Other\\NSpace

```

`output-path` can be absolute path or relative to project's `app` directory. Absolute path must start with `/`:

- `/var/www/html/app/Models` - absolute path
- `Models` - relative path, will be transformed to `/var/www/html/app/Models` (assuming your project app directory is `/var/www/html/app`)

### base-class-name

[](#base-class-name)

By default generated class will be extended from `Illuminate\Database\Eloquent\Model`. To change the base class specify `base-class-name` option:

```
php artisan generate:model User --base-class-name=Some\\Other\\Base\\Model

```

### backup

[](#backup)

Save existing model before generating a new one

```
php artisan generate:model User --backup

```

If `User.php` file already exist, it will be renamed into `User.php~` first and saved at the same directory. After than a new `User.php` will be generated.

### Other options

[](#other-options)

There are several useful options for defining several model's properties:

- `no-timestamps` - adds `public $timestamps = false;` property to the model
- `date-format` - specifies `dateFormat` property of the model
- `connection` - specifies connection name property of the model

### Overriding default options globally

[](#overriding-default-options-globally)

Instead of spcifying options each time when executing the command you can create a config file named `eloquent_model_generator.php` at project's `config` directory with your own default values. Generator already contains its own config file at `Resources/config.php` with following options:

```
