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:
- Models
- Migrations
- Resources
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!
var/www/moonshine-builder/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": [
{
"CategoryResource": {
"fields": {
"id": {
"type": "id",
"methods": [
"sortable"
]
},
"title": {
"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": [
{
"CategoryResource": {
"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