dev-lnk / moonshine-builder
Project builder for MoonShine
Requires
- php: ^8.2
- dev-lnk/laravel-code-builder: ^v1.0
Requires (Dev)
- moonshine/moonshine: ^3.0
- orchestra/testbench: ^9.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^11.0
README
Generate code using JSON schema, SQL tables, and console commands for MoonShine.
Table of contents
- Description
- Installation
- Generate code using JSON or MYSQL
- Creating a JSON schema
- Creation from SQL table
- Generating code using console command
Description
Hello, Laravel and MoonShine User!
This package allows you to describe the entire project structure using a JSON table schema, SQL
table, console command 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') ];
Generate code using JSON or MYSQL
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, for 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 JSON 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. For hints in your IDE or for a more detailed description of the JSON structure, you can use this file.
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 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 }
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
.
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 generated, which you can edit and apply if desired.
project_20240613113014.json was created successfully! To generate resources, run:
php artisan moonshine:build project_20240613113014.json
Generating code using console command
To generate the resource, model, and migration, run the command:
php artisan ms-build:resource Post
Specify all required fields:
┌ Column: ─────────────────────────────────────────────────────┐
│ id │
└──────────────────────────────────────────────────────────────┘
┌ Column name: ────────────────────────────────────────────────┐
│ Id │
└──────────────────────────────────────────────────────────────┘
┌ Column type: ────────────────────────────────────────────────┐
│ id │
├──────────────────────────────────────────────────────────────┤
│ › id ┃ │
│ string │ │
│ text │ │
│ boolean │ │
│ bigInteger │ │
│ BelongsTo │ │
│ HasMany │ │
│ HasOne │ │
│ BelongsToMany │ │
└──────────────────────────────────────────────────────────────┘
┌ Add more fields? ────────────────────────────────────────────┐
│ ● Yes / ○ No │
└──────────────────────────────────────────────────────────────┘
Once all fields have been specified, generate the code
┌ Add timestamps? ─────────────────────────────────────────────┐ │ Yes │ └──────────────────────────────────────────────────────────────┘ ┌ Add softDelete? ─────────────────────────────────────────────┐ │ No │ └──────────────────────────────────────────────────────────────┘ ┌ Make migration? ─────────────────────────────────────────────┐ │ Yes │ └──────────────────────────────────────────────────────────────┘ app/Models/Post.php was created successfully! app/MoonShine/Resources/PostResource.php was created successfully! migrations/2024_09_10_111121_create_posts.php was created successfully!
Specifying fields in the command
You can also specify all fields at once in the command, ignoring the interactive mode:
php artisan ms-build:resource Post id:ID:id name:string status_id:Status:BelongsTo:statuses
The structure of the value has the following scheme: <column>:<column name>:<type>
If the field type is relation, the structure is as follows: <column>(name of the relationship method in the model):<column name>:<type>:<table>
List of field types
The list of field types can be retrieved using the command:
php artisan ms-build:types