2017 © Pedro Peláez
 

library generators

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

image

laracademy/generators

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  • Monday, October 16, 2017
  • by Laracademy
  • Repository
  • 12 Watchers
  • 214 Stars
  • 12,339 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 22 Forks
  • 5 Open issues
  • 9 Versions
  • 19 % Grown

The README.md

Laracademy Generators

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Laracademy Generators - is a tool set that helps speed up the development process of a Laravel application., (*2)

Author(s): * Laracademy (@laracademy, michael@laracademy.co), (*3)

Requirements

  1. PHP 7.4+
  2. Laravel 6.*
  3. MySQL *

** For Laravel 5.* please use the version 1.5, (*4)

Usage

Step 1: Install through Composer

composer require "laracademy/generators" --dev

Step 2: Artisan Command

Now that we have added the generator to our project the last thing to do is run Laravel's Arisan command, (*5)

php artisan

You will see the following in the list, (*6)

generate:modelfromtable

Commands

generate:modelfromtable

This command will read your database table and generate a model based on that table structure. The fillable fields, casts, dates and even namespacing will be filled in automatically., (*7)

You can use this command to generate a single table, multiple tables or all of your tables at once., (*8)

This command comes with a bunch of different options, please see below for each parameter, (*9)

  • --table=
    • This parameter if filled in will generate a model for the given table
    • You can also pass in a list of tables using comma separated values
    • When omitted, all tables will generate a model
    • In this scenario you can optionally specify a whitelist/blacklist in config/modelfromtable.php
    • migrations table will be blacklisted by default
  • --connection=
    • By default, if omitted, the default connection found in config/database.php will be used
    • To specify a connection, first ensure that it exists in your config/database.php
  • --folder=
    • By default, all models are store in your app/ directory. If you wish to store them in another place you can provide the relative path from your base laravel application.
    • Alternatively, use a lambda in config/modelfromtable.php to dynamically specify folder path
  • --namespace=
    • By default, all models will have the namespace of App\Models
    • Alternatively, use a lambda in config/modelfromtable.php to dynamically specify namespace
  • --debug=[true|false (default)]
    • Shows some more information while running
  • --singular=[true|false (default)]
    • This will create a singular titled model, e.g. "Categories" -> "Category"
  • --overwrite=[true|false (default)]
    • Overwrite model file(s) if exists
  • --timestamps=[true|false (default)]
    • whether to timestamp or not

Examples (CLI)

Generating a single table

php artisan generate:modelfromtable --table=users

Generating a multiple tables

php artisan generate:modelfromtable --table=users,posts

Changing to another connection found in database.php

php artisan generate:modelfromtable --connection=spark

Changing the folder where to /app/Models

php artisan generate:modelfromtable --table=user --folder=app/Models

Configuration file for saving defaults, dynamic lambdas

A config file should be in your project's config folder (if not, you can easily create it). Through this, you can set defaults you commonly use to cut down on the input your command line call requires. Some fields, like namespace, accept a static value or, more powerfully, a lambda to generate dynamic values. Additional fields not available to the CLI are available in the config. See below., (*10)

Whitelist/Blacklist (config only)

Particularly large databases often have a number of tables that aren't meant to have models. These can easily be filtered through either the whitelist or blacklist (or both!). Laravel's "migrations" table is already included in the blacklist. One nice feature is that you can wildcard table names if that makes sense for your situation..., (*11)

'blacklist' => ['migrations'];
'whitelist' => ['user_address', 'system_*'];

Filename, using lambda

Large databases sometimes use a pattern of prefixing for organization, which you can use to organize your model files through a lambda., (*12)

'filename' => fn(string $tableName) => Str::studly(Str::after($tableName, '_')),

In this example, 'system_user' would generate the filename 'User'. Note that this is also available through the CLI, but it probably doesn't make as much sense to set there., (*13)

Folder path, using lambda

Using the last example, you can also organize the folder path using the prefix..., (*14)

'folder' => (function (string $tableName) {
    $prefix = Str::studly(Str::before($tableName, '_'));
    $path = app()->path('Models') . "/{$prefix}";

    if (!is_dir($path)) {
        mkdir($path);
    }

    return $path;
}),

In this example, 'system_user' would generate the folder path 'path/to/your/install/app/Models/System', (*15)

Namespace, using lambda

Using the last example, you would want to then generate a matching namespace to the file path, (*16)

'namespace' => fn(string $folderpath) => 'App/Models' . Str::after($folderpath, app()->path('Models')),

Therefore the folder path 'path/to/your/install/app/Models/System' would generate the namespace 'App\Models\System', (*17)

Delimiter (config only)

By default array values are delimited with a simple comma, but a common preference is to delimit with a newline as well., (*18)

'delimiter' => ",\n" . str_repeat(' ', 8),

Result:, (*19)

class SomeModel extends Model
{
    protected $fillable = [
        'SomeField',
        'AnotherField',
        'YetAnotherField'
    ];

License

ModelGen is open-sourced software licensed under the MIT license, (*20)

Bug Reporting and Feature Requests

Please add as many details as possible regarding submission of issues and feature requests, (*21)

Disclaimer

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*22)

The Versions

16/10 2017

dev-master

9999999-dev

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

16/10 2017

1.3.3

1.3.3.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

21/01 2017

1.3.2

1.3.2.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

07/09 2016

1.3.1

1.3.1.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

07/09 2016

1.3

1.3.0.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

07/09 2016

1.2.1

1.2.1.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

07/09 2016

1.2

1.2.0.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

06/06 2016

1.1

1.1.0.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators

28/05 2016

1.0

1.0.0.0

This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.

  Sources   Download

MIT

by Michael McMullen

laravel generators