Creating projects using schemas for the MoonShine.
- Description
- Installation
- Configuration
- Quick start
- Code generation methods
- Bulk table import
- Use in other projects
This package allows you to create Resource, Model and Migration with all fields using generation methods from:
- SQL table,
- JSON schema,
- Code generation for a new resource from the console,
- Existing Laravel model.
The package generates the following files:
composer require dev-lnk/moonshine-builder --devPublish the package configuration file:
php artisan vendor:publish --tag=moonshine-builderIn 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,
];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, availabletable,json,console,model.
Individual commands are also available for each generation type:
php artisan moonshine:build-json {target?}- generation from JSON schemaphp artisan moonshine:build-table {target?}- generation from SQL tablephp artisan moonshine:build-resource {entity?} {fields?*}- console generationphp artisan moonshine:build-model {entity?} {--all}- generation from existing model
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=tableOr use a specialized command:
php artisan moonshine:build-table usersResult:
/**
* @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'),
];
}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.jsonOr use a specialized command:
php artisan moonshine:build-json category.jsonA more detailed example with multiple resources and relationships can be found here.
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.
Works similarly to the timestamps flag and the deleted_at field.
Using the withResource, withModel, withMigration flags, you can configure what exactly needs to be generated for your resource:
{
"name": "ItemPropertyPivot",
"withResource": false,
"withModel": false
}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:stringResult:
/**
* @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
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 Productor 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 --allThis 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',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-schemaFirst, 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
- MoonVibe - admin panel generation using AI
