Skip to content

Latest commit

 

History

History
402 lines (328 loc) · 17.6 KB

File metadata and controls

402 lines (328 loc) · 17.6 KB

logo

Creating projects using schemas for the MoonShine.

Latest Stable Version Total Downloads tests License
Laravel required PHP required MoonShine required


Description

This package allows you to create Resource, Model and Migration with all fields using generation methods from:

The package generates the following files:

Installation

composer require dev-lnk/moonshine-builder --dev

Configuration

Publish the package configuration file:

php artisan vendor:publish --tag=moonshine-builder

In the configuration file, specify the path to your JSON schemas:

return [
    // Directory where schematic files in json, yaml, etc. are stored.
    'builds_dir' => base_path('builds'),

    // Base path for models directory.
    'base_model_path' => 'app/Models',

    // Notification of duplicate files of models and resources with a new generation.
    'is_confirm_replace_files' => true,

    // Ask about adding a new resource to the provider.
    'is_confirm_change_provider' => false,

    // Ask about adding a new resource to the menu.
    'is_confirm_change_menu' => false,
];

Quick start

Run the command:

php artisan moonshine:build

You will be given options as to which scheme to use when generating the code, for example:

 ┌ Type ────────────────────────────────────────────────────────┐
 │   ○ table                                                    │
 │ › ● json                                                     │
 │   ○ console                                                  │
 │   ○ model                                                    │
 └──────────────────────────────────────────────────────────────┘

When selecting the json option:

 ┌ File ────────────────────────────────────────────────────────┐
 │ › ● category.json                                            │
 │   ○ project.json                                             │
 └──────────────────────────────────────────────────────────────┘
app/Models/Category.php was created successfully!
app/MoonShine/Resources/CategoryResource.php was created successfully!
database/migrations/2024_05_27_140239_create_categories.php was created successfully!

INFO  All done.

The command has the following signature moonshine:build {target?} {--type=}, where:

  • target - entity for which generation will be performed,
  • type - type or generation method, available table, json, console, model.

Individual commands are also available for each generation type:

  • php artisan moonshine:build-json {target?} - generation from JSON schema
  • php artisan moonshine:build-table {target?} - generation from SQL table
  • php artisan moonshine:build-resource {entity?} {fields?*} - console generation
  • php artisan moonshine:build-model {entity?} {--all} - generation from existing model

Code generation methods

Creation from SQL table

You can create a resource using a table schema. To do this, run the command php artisan moonshine:build and select the table option:

 ┌ Type ────────────────────────────────────────────────────────┐
 │ › ● table                                                    │
 │   ○ json                                                     │
 │   ○ console                                                  │
 └──────────────────────────────────────────────────────────────┘

Select the required table:

 ┌ Table ───────────────────────────────────────────────────────┐
 │   ○ password_reset_tokens                                  │ │
 │   ○ sessions                                               │ │
 │   ○ statuses                                               │ │
 │   ○ tasks                                                  │ │
 │ › ● users                                                  ┃ │
 └──────────────────────────────────────────────────────────────┘

You can immediately specify the table name and generation type. Example:

php artisan moonshine:build users --type=table

Or use a specialized command:

php artisan moonshine:build-table users

Result:

/**
 * @return list<ComponentContract|FieldContract>
 */
protected function fields(): iterable
{
    return [
        ID::make('id'),
        Text::make('name', 'name'),
        Text::make('email', 'email'),
        Date::make('email_verified_at', 'email_verified_at'),
        Text::make('password', 'password'),
        Text::make('remember_token', 'remember_token'),
    ];
}

Creation from JSON schema

JSON structure JSON. In the builds_dir directory, create a schema file, for example, category.json:

{
  "resources": [
    {
      "name": "Category",
      "fields": [
        {
          "column": "id",
          "type": "id",
          "methods": [
            "sortable"
          ]
        },
        {
          "column": "name",
          "type": "string",
          "name": "Name"
        }
      ]
    }
  ]
}

To generate project files, run the command:

php artisan moonshine:build category.json

Or use a specialized command:

php artisan moonshine:build-json category.json

A more detailed example with multiple resources and relationships can be found here.

Timestamps

You can specify the timestamps: true flag:

{
  "resources": [
    {
      "name": "Category",
      "timestamps": true,
      "fields": []
    }
  ]
}

The created_at and updated_at fields will be added to the generated code. If you manually specify the created_at and updated_at fields, the timestamps flag will be automatically set to true.

Soft delete

Works similarly to the timestamps flag and the deleted_at field.

Flags for generating files

Using the withResource, withModel, withMigration flags, you can configure what exactly needs to be generated for your resource:

{
  "name": "ItemPropertyPivot",
  "withResource": false,
  "withModel": false
}

Console generation

Run the command php artisan moonshine:build and select the console option, or run the moonshine:build-resource command. Next, you need to specify the resource name and describe all fields:

 ┌ Type ────────────────────────────────────────────────────────┐
 │ console                                                      │
 └──────────────────────────────────────────────────────────────┘

 ┌ Resource name: ──────────────────────────────────────────────┐
 │ Status                                                       │
 └──────────────────────────────────────────────────────────────┘

 ┌ Column: ─────────────────────────────────────────────────────┐
 │ id                                                           │
 └──────────────────────────────────────────────────────────────┘

 ┌ Column name: ────────────────────────────────────────────────┐
 │ Id                                                           │
 └──────────────────────────────────────────────────────────────┘

 ┌ Column type: ────────────────────────────────────────────────┐
 │ id                                                           │
 └──────────────────────────────────────────────────────────────┘

 ┌ Add more fields? ────────────────────────────────────────────┐
 │ ● Yes / ○ No                                                 │
 └──────────────────────────────────────────────────────────────┘

You can immediately create a resource with fields by running the following command:

php artisan moonshine:build-resource Status id:Id:id name:Name:string

Result:

/**
 * @return list<ComponentContract|FieldContract>
 */
protected function fields(): iterable
{
    return [
        ID::make('id'),
        Text::make('Name', 'name'),
    ];
}

Command signature moonshine:build-resource {entity?} {fields?*}, where:

  • entity - resource name,
  • fields - fields for generation like name:Name:string or {column}:{columnName}:{type}

All available {type} can be viewed by running the command php artisan moonshine:build-types

Generation from existing model

If you already have a ready-made Laravel model with defined fields, relationships, and settings, you can generate a MoonShine Resource based on that model. Run the command php artisan moonshine:build and select the model option:

 ┌ Type ────────────────────────────────────────────────────────┐
 │   ○ table                                                    │
 │   ○ json                                                     │
 │   ○ console                                                  │
 │ › ● model                                                    │
 └──────────────────────────────────────────────────────────────┘

Then select the required model from the list of available models:

 ┌ Select models (use Space to select, Enter to confirm): ──────┐
 │   ◻ app/Models/Category.php                                │ │
 │   ◻ app/Models/Comment.php                                 │ │
 │   ◻ app/Models/Product.php                                 │ │
 │   ◻ app/Models/Rating.php                                  │ │
 │   ◻ app/Models/Review.php                                  │ │
 │   ◻ app/Models/Tag.php                                     │ │
 └────────────────────────────────────────────────── 0 selected ┘

You can also directly specify the model for generation:

php artisan moonshine:build-model Product

or with the full class name:

php artisan moonshine:build-model "App\Models\Product"

Generating resources for all models

If you need to create resources for all models in the directory, use the --all flag:

php artisan moonshine:build-model --all

This command will automatically scan the models directory and create resources for each model found.

The package will automatically analyze the model and create:

  • Resource with fields based on the table structure
  • Relationships (HasMany, BelongsTo, BelongsToMany, HasOne) based on model methods
  • Correct field types based on column types in the database
  • Timestamps and soft deletes settings if they are used in the model

Configuring the models directory

By default, the package looks for models in the app/Models directory. You can change this in the configuration file:

'base_model_path' => 'app/Models',

Bulk table import

If you already have a project with its own database and you don't want to build the resources one by one, you can use the following command:

php artisan moonshine:project-schema

First, select all your Pivot tables to correctly form the BelongsToMany relationship, then select all the necessary tables for which you want to generate resources.

 ┌ Select the pivot table to correctly generate BelongsToMany (Press enter to skip) ┐
 │ item_property                                                                    │
 └──────────────────────────────────────────────────────────────────────────────────┘

 ┌ Select tables ───────────────────────────────────────────────┐
 │ categories                                                   │
 │ comments                                                     │
 │ items                                                        │
 │ products                                                     │
 │ properties                                                   │
 │ users                                                        │
 └──────────────────────────────────────────────────────────────┘

A JSON schema will be created, which you can edit and use if desired:

project_20240613113014.json was created successfully! To generate resources, run: 
php artisan moonshine:build project_20240613113014.json

Use in other projects

  • MoonVibe - admin panel generation using AI