Creating projects using schemas for the MoonShine.
This package allows you to describe the entire project structure using a JSON or SQL
table schema and generate the necessary files, such as:
composer require dev-lnk/moonshine-builder --dev
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.
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.
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
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
Works similarly to the timestamps
flag and the deleted_at
field
Using flags withResource
, withModel
, withMigration
, you can configure what exactly you want to generate for your resource
{
"name": "ItemPropertyPivot",
"withResource": false,
"withModel": false
}
For hints in your IDE or for a more detailed description of the json structure, you can use this file
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