2017 © Pedro Peláez
 

library eloquent-model-generator

Eloquent Model Generator

image

krlove/eloquent-model-generator

Eloquent Model Generator

  • Saturday, July 28, 2018
  • by krlove
  • Repository
  • 15 Watchers
  • 133 Stars
  • 54,125 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 41 Forks
  • 3 Open issues
  • 13 Versions
  • 21 % Grown

The README.md

Eloquent Model Generator

Eloquent Model Generator generates Eloquent models using database schema as a source., (*1)

Version 2.0.0

Version 2.0.0 has been released. Checkout this discussion for more details and upgrade instructions., (*2)

Installation

Step 1. Add Eloquent Model Generator to your project:, (*3)

composer require krlove/eloquent-model-generator --dev

Step 2. Register GeneratorServiceProvider:, (*4)

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

Step 3. Configure your database connection., (*5)

Usage

Use, (*6)

php artisan krlove:generate:model User

to generate a model class. Generator will look for table named users and generate a model for it., (*7)

table-name

Use table-name option to specify another table name:, (*8)

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

In this case generated model will contain protected $table = 'user' property., (*9)

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:, (*10)

php artisan krlove:generate:model User --output-path=/full/path/to/output/directory --namespace=Your\\Custom\\Models\\Place

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 - Custom/Models - relative path, will be transformed to /var/www/html/app/Custom/Models (assuming your project app directory is /var/www/html/app), (*11)

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:, (*12)

php artisan krlove:generate:model User --base-class-name=Custom\\Base\\Model

no-backup

If User.php file already exist, it will be renamed into User.php~ first and saved at the same directory. Unless no-backup option is specified:, (*13)

php artisan krlove:generate:model User --no-backup

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, (*14)

Overriding default options

Instead of specifying 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:, (*15)

<?php

return [
    'namespace' => 'App',
    'base_class_name' => \Illuminate\Database\Eloquent\Model::class,
    'output_path' => null,
    'no_timestamps' => null,
    'date_format' => null,
    'connection' => null,
    'no_backup' => null,
    'db_types' => null,
];

Registering custom database types

If running a command leads to an error, (*16)

[Doctrine\DBAL\DBALException]
Unknown database type <TYPE> requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.

it means that you must register your type <TYPE> at your config/eloquent_model_generator.php:, (*17)

return [
    // ...
    'db_types' => [
        '<TYPE>' => 'string',
    ],
];

Usage example

Table user:, (*18)

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `email` varchar(100) NOT NULL,
  `role_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `role_id` (`role_id`),
  CONSTRAINT `user_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Command:, (*19)

php artisan krlove:generate:model User

Result:, (*20)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @property int $id
 * @property int $role_id
 * @property mixed $username
 * @property mixed $email
 * @property Role $role
 * @property Article[] $articles
 * @property Comment[] $comments
 */
class User extends Model
{
    /**
     * @var array
     */
    protected $fillable = ['role_id', 'username', 'email'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function role()
    {
        return $this->belongsTo('Role');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function articles()
    {
        return $this->hasMany('Article');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function comments()
    {
        return $this->hasMany('Comment');
    }
}

Generating models for all tables

Command krlove:generate:models will generate models for all tables in the database. It accepts all options available for krlove:generate:model along with skip-table option., (*21)

skip-table

Specify one or multiple table names to skip:, (*22)

php artisan krlove:generate:models --skip-table=users --skip-table=roles

Note that table names must be specified without prefix if you have one configured., (*23)

Customization

You can hook into the process of model generation by adding your own instances of Krlove\EloquentModelGenerator\Processor\ProcessorInterface and tagging it with GeneratorServiceProvider::PROCESSOR_TAG., (*24)

Imagine you want to override Eloquent's perPage property value., (*25)

class PerPageProcessor implements ProcessorInterface
{
    public function process(EloquentModel $model, Config $config): void
    {
        $propertyModel = new PropertyModel('perPage', 'protected', 20);
        $dockBlockModel = new DocBlockModel('The number of models to return for pagination.', '', '@var int');
        $propertyModel->setDocBlock($dockBlockModel);
        $model->addProperty($propertyModel);
    }

    public function getPriority(): int
    {
        return 8;
    }
}

getPriority determines the order of when the processor is called relative to other processors., (*26)

In your service provider:, (*27)

public function register()
{
    $this->app->tag([InflectorRulesProcessor::class], [GeneratorServiceProvider::PROCESSOR_TAG]);
}

After that, generated models will contain the following code:, (*28)

/**
 * The number of models to return for pagination.
 * 
 * @var int
 */
protected $perPage = 20;

The Versions

28/07 2018
24/07 2018
15/07 2018

1.2.7

1.2.7.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

08/10 2017

1.2.6

1.2.6.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

19/07 2017

1.2.5

1.2.5.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

23/06 2017

1.2.4

1.2.4.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

19/03 2017

1.2.3

1.2.3.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

20/11 2016

1.2.1

1.2.1.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

20/08 2016

1.2.0

1.2.0.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

15/07 2016

1.1.0

1.1.0.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires

 

03/04 2016

1.0.0

1.0.0.0

Eloquent Model Generator

  Sources   Download

MIT

The Requires