dev-lnk/moonshine-builder

MoonShine的项目构建器

1.2 2024-09-10 11:31 UTC

This package is auto-updated.

Last update: 2024-09-10 11:36:05 UTC


README

logo

使用JSON模式、SQL表和命令行生成MoonShine的代码。

Latest Stable Version tests License

Laravel required PHP required MoonShine required

目录

描述

大家好,Laravel和MoonShine用户!

此包允许您使用JSON表模式、SQL表、控制台命令来描述整个项目结构,并生成必要的文件,例如

安装

composer require dev-lnk/moonshine-builder --dev

配置

发布包配置文件

php artisan vendor:publish --tag=moonshine-builder

在配置文件中指定JSON模式的路径

return [
    'builds_dir' => base_path('builds')
];

使用JSON或MYSQL生成代码

现在您可以运行以下命令

php artisan moonshine:build

您将获得选项,以选择在生成代码时使用哪种方案,例如

 ┌ 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.

创建JSON模式

builds_dir目录中创建一个模式文件,例如,category.json

{
  "resources": [
    {
      "name": "Category",
      "fields": [
        {
          "column": "id",
          "type": "id",
          "methods": [
            "sortable"
          ]
        },
        {
          "column": "name",
          "type": "string",
          "name": "Name"
        }
      ]
    }
  ]
}

要生成项目文件,请运行以下命令

 php artisan moonshine:build category.json

有关多个资源和关系的更详细示例,请参阅此处。有关您的IDE中的提示或JSON结构的更详细描述,您可以使用此文件

时间戳

您可以指定timestamps: true标志

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

将添加created_atupdated_at字段到您的代码中。如果您手动指定了created_atupdated_at字段,则timestamps标志将自动设置为true。

软删除

timestamps标志和deleted_at字段类似。

生成文件的标志

使用withResourcewithModelwithMigration标志,您可以配置为为资源生成什么

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

从SQL表创建

您可以使用表模式创建资源。您必须指定表名并选择table类型。例如

php artisan moonshine:build users --type=table

结果

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'),
        ]),
    ];
}

生成文件后,请确保在您的MoonShineServiceProvider中注册所有新的资源。

批量表导入

如果您已经有一个具有自己数据库的项目,并且不想逐个构建资源,您可以使用以下命令

php artisan moonshine:project-schema

首先,选择所有您的Pivot表以正确形成多对多关系,然后选择您想要生成资源的所有必要表。

 ┌ Select the pivot table to correctly generate BelongsToMany (Press enter to skip) ┐
 │ item_property                                                                    │
 └──────────────────────────────────────────────────────────────────────────────────┘

 ┌ Select tables ───────────────────────────────────────────────┐
 │ categories                                                   │
 │ comments                                                     │
 │ items                                                        │
 │ products                                                     │
 │ properties                                                   │
 │ users                                                        │
 └──────────────────────────────────────────────────────────────┘

将生成一个JSON模式,您可以根据需要编辑并应用。

project_20240613113014.json was created successfully! To generate resources, run: 
php artisan moonshine:build project_20240613113014.json

使用控制台命令生成代码

要生成资源、模型和迁移,请运行以下命令

php artisan ms-build:resource Post

指定所有必需的字段

 ┌ Column: ─────────────────────────────────────────────────────┐
 │ id                                                           │
 └──────────────────────────────────────────────────────────────┘

 ┌ Column name: ────────────────────────────────────────────────┐
 │ Id                                                           │
 └──────────────────────────────────────────────────────────────┘

 ┌ Column type: ────────────────────────────────────────────────┐
 │ id                                                           │
 ├──────────────────────────────────────────────────────────────┤
 │ › id                                                       ┃ │
 │   string                                                   │ │
 │   text                                                     │ │
 │   boolean                                                  │ │
 │   bigInteger                                               │ │
 │   BelongsTo                                                │ │
 │   HasMany                                                  │ │
 │   HasOne                                                   │ │
 │   BelongsToMany                                            │ │
 └──────────────────────────────────────────────────────────────┘
 ┌ Add more fields? ────────────────────────────────────────────┐
 │ ● Yes / ○ No                                                 │
 └──────────────────────────────────────────────────────────────┘

指定所有字段后,生成代码

 ┌ 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!

指定命令中的字段

您也可以一次在命令中指定所有字段,忽略交互式模式

php artisan ms-build:resource Post id:ID:id name:string status_id:Status:BelongsTo:statuses

值的结构具有以下模式:<column>:<column name>:<type>

如果字段类型是关系,结构如下:<column>(模型中关系方法的名称):<column name>:<type>:<table>

字段类型列表

可以使用命令检索字段类型的列表

php artisan ms-build:types