Skip to content

dev-lnk/laravel-code-builder

Repository files navigation

Generating laravel code from SQL table schema

Latest Stable Version Total Downloads tests License
Laravel required PHP required

Description

Hello Laravel users! This package allows you to generate code from the schema of your SQL table. The following entities will be generated:

These examples have been generated from a table created by migration:

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('title')->default('Default');
    $table->text('content');
    $table->foreignIdFor(User::class)
        ->nullable()
        ->constrained()
        ->nullOnDelete()
        ->cascadeOnUpdate();
    $table->smallInteger('sort_number')->default(0);
    $table->boolean('is_active')->default(0);
    $table->timestamps();
    $table->softDeletes();
});

What is this package for?

This package allows you to significantly reduce the routine while coding and focus on developing.

Installation

composer require dev-lnk/laravel-code-builder --dev

Configuration:

Publish the package configuration file:

php artisan vendor:publish --tag=laravel-code-builder

Usage

The basic command signature looks like this:

code:build {entity} {table?}

Let's say we want to create classes for the base table users based on the User entity. To do this you need to run the following command:

php artisan code:build User

You will be presented with a list of your tables, choose which table you want to generate the code based on:

 ┌ Table ───────────────────────────────────────────────────────┐
 │   ○ migrations                                             │ │
 │   ○ password_reset_tokens                                  │ │
 │   ○ products                                               │ │
 │   ○ sessions                                               │ │
 │ › ● users                                                  ┃ │
 └──────────────────────────────────────────────────────────────┘

You can also specify part of the table name to shorten the list

php artisan code:build User us
 ┌ Table ───────────────────────────────────────────────────────┐
 │ › ● users                                                    │
 └──────────────────────────────────────────────────────────────┘

If you have not specified a generation_path in the configuration file, you will be offered 2 options:

 ┌ Where to generate the result? ───────────────────────────────┐
 │ › ● In the project directories                               │
 │   ○ To the generation folder: `app/Generation`               │
 └──────────────────────────────────────────────────────────────┘

The first option will create all files according to the folders in your app_path directory. If a file with the same name is found, you will be prompted to replace it:

app/Models/User.php was created successfully!
...
 ┌ Controller already exists, are you sure you want to replace it? ┐
 │ Yes                                                             │
 └─────────────────────────────────────────────────────────────────┘

app/Http/Controllers/UserController.php was created successfully!
...

In the second option, all files will be generated in the app/Generation folder

app/Generation/Models/User.php was created successfully!
...

In the builders configuration you can comment out those builders that you do not want to be executed

return [
    'builders' => [
        'model',
//        'addAction',
//        'editAction',
//        'request',
//        'controller',
//        'route',
        'form',
//        'DTO',
//        'table',
    ],
    //...
];

You can generate certain entities using flags:

php artisan code:build user --model --request

Available options for the only flag:

  • --model
  • --request
  • --DTO
  • --addAction
  • --editAction
  • --controller
  • --route
  • --form
  • --table
  • --typeScript
  • --builder - Generates all builders specified in the builders configuration + your specified flag, for example:
php artisan code:build user --builders --request

Documentation