dev-lnk/moonshine-builder

Project builder for MoonShine

1.1.1 2024-06-13 12:26 UTC

This package is auto-updated.

Last update: 2024-06-13 12:27:50 UTC


README

logo

Creating projects using schemas for the MoonShine.

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

Hello, Laravel and MoonShine User!

This package allows you to describe the entire project structure using a JSON or SQL table schema and generate the necessary files, such as:

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 [
    'builds_dir' => base_path('builds')
];

Now you can run the command:

php artisan moonshine:build

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

 ┌ Type ────────────────────────────────────────────────────────┐
 │ › ● json                                                     │
 │   ○ table                                                    │
 └──────────────────────────────────────────────────────────────┘
 ┌ 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!

WARN  Don't forget to register new resources in the provider method:

 new CategoryResource(),

 ...or in the menu method:

 MenuItem::make(
     static fn() => 'CategoryResource',
     new CategoryResourceResource()
 ),

INFO  All done.

Creating a Schema

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

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

Creation from sql table

You can create a resource using a table schema.You must specify the table name and select table type. Example:

php artisan moonshine:build users --type=table

Result:

public function fields(): array
{
    return [
        Block::make([
            ID::make('id'),
            Text::make('Name', 'name'),
            Text::make('Email', 'email'),
            Date::make('EmailVerifiedAt', 'email_verified_at'),
            Text::make('Password', 'password'),
            Text::make('RememberToken', 'remember_token'),
        ]),
    ];
}

After generating the files, make sure to register all new Resources in your MoonShineServiceProvider

Timestamps

You can specify the timestamp: true flag

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

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

Soft deletes

Works similarly to the timestamps flag and the deleted_at field

Flags for generating files

Using flags withResource, withModel, withMigration, you can configure what exactly you want to generate for your resource

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

JSON Schema

For hints in your IDE or for a more detailed description of the json structure, you can use this file