Blog Detail

11

Oct
Generate Eloquent Models using Database Schema as a Source cover image

arrow_back Generate Eloquent Models using Database Schema as a Source

Eloquent Model Generator generates Eloquent models using database schema as a source.

Installation

Step 1. Add Eloquent Model Generator to your project:

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

Step 2. Register GeneratorServiceProvider:

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

Usage

Use the command below to generate a model class. The generator will look for table-named users and generate a model for them.

php artisan krlove:generate:model User

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

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

In this case generated model will contain protected $table = ‘user’ property.

output-path

The generated file will be saved into the 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 krlove:generate:model User --output-path=/full/path/to/output/directory --namespace=Your\\Custom\\Models\\Place

output-path can be an absolute path or relative to the project’s app directory. The 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)

base-class-name

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

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

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

Instead of specifying options each time when executing the command, you can create a config file named eloquent_model_generator.php in the project’s config directory with your own default values:

<?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

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

it means that you must register your type at your config/eloquent_model_generator.php:

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

For more details, Please visit Github.

Published at : 11-10-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project