bfg/scaffold

递归创建迁移模型及其之间关系的脚手架

1.12.0 2022-05-12 20:24 UTC

README

该包的本质是为进一步处理和为laravel工作而设计数据库和初始数据的可视化语法。

更多细节

一个根据单个语法创建laravel层的包,该语法基于一个简单的数组,该数组代表一组规则和条件,这些规则和条件是构建项目架构初始数据所必需的。我们谈论的是像:模型模型常量模型关系模型属性模型行为(特质)转换迁移观察者请求资源规则工厂播种器等。

安装

您可以使用composer安装此库

composer require bfg/scaffold

工作开始

如果不存在文件 /database/scaffolds.json,则在运行命令 php artisan scaffold 的第一次运行后,此文件将自动创建!然后您需要填写生成数据并重新运行命令 php artisan scaffold

JSON语法

首先,您需要了解如何为您的项目设计脚手架数组。

您必须完全理解整个项目,以便在同一个地方最正确地设计您的项目和数据库。

让我们考虑一种简单的设计方法,这是一个 JSON 文件。

这种方法简化了集成和快速实施。

学前班数据库结构的简单示例

{
  "required": ["user/location"],
  "commentary": {
    "prop:translatable": ["name"],
    "const:title": "Comments",
    "type": "morphMany:commentable",
    "fields": [
      ["name", ["nullable"]],
      ["src", ["nullable"]],
      "active"
    ],
    "rules": {
      "name": "required|string|min:10|max:191",
      "src": "nullable|string|Uppercase"
    },
    "factory": {
      "name": "faker.unique().name()",
      "src": "faker.imageUrl(640, 480, 'animals', true)"
    },
    "seed": "factory:30,50"
  },
  "rules": {
    "const:title": "Rules",
    "fields": ["title", "text", "active"],
    "relations": {
      "commentary": {}
    },
    "seed": [
      {
        "title": "Warning 1",
        "text": "Warning text 1"
      },
      {
        "title": "Warning 2",
        "text": "Warning text 2"
      }
    ]
  },
  "director": {
    "const:title": "Directors",
    "auth": true,
    "traits": ["Notifiable", "SoftDeletes", "Getter", "Setter", "Scope"],
    "fields": ["name", "last_name", "active"],
    "relations": {
      "commentary": {},
      "reward:hasMany": {
        "const:title": "Rewards",
        "fields": ["name", "license", "photo", "handed_over_at", "active"]
      }
    },
    "seed": {
      "name": "Name", 
      "last_name": "Last Name"
    }
  },
  "school": {
    "const:title": "Schools",
    "traits": ["Getter", "Setter", "Scope", "Wets"],
    "fields": ["name", "description", "active", "active"],
    "relations": {
      "director": {},
      "commentary": {},
      "group:hasMany": {
        "const:title": "Groups",
        "fields": ["name", ["slogan", ["nullable"]], "active"],
        "relations": {
          "commentary": {},
          "educator:belongsToMany": {
            "const:title": "Educators",
            "fields": ["name", "last_name", "active"],
            "relations": {
              "commentary": {}
            }
          }
        }
      },
      "service:belongsToMany": {
        "const:title": "Services",
        "fields": ["name", "amount", "description", "active"],
        "relations": {
          "commentary": {}
        }
      },
      "tags:belongsToMany": {
        "const:title": "Tags",
        "fields": ["name", "active", "is_new", "mark_at"]
      }
    }
  }
}

为了发布此演示

php artisan vendor:publish --tag=scaffold-demo

现在更详细地介绍数组

{
  "{Singular model name}": { //All fields except names are optional
    "const:{Constant name}": "Constant value", //optional
    "created": false, // Cancel created at support
    "updated": false, // Cancel updated at support
    "path": "app/Models", //by default
    "prop:translatable": ["title"], //For make custom model property
    "namespace": "App\\Models", //by default
    "foreign": "id", //by default
    "observer": [], //observer for model, if the array of values is empty, add all possible events. or list what events are needed in an array.
    "timestamps": true, //by default
    "traits": ["Getter", "Setter", "Scope", "Wets"], //Injected traits. optional
    "type": "{Type name}:{Type parameters}", //The parent type when adding this model as a link. by default hasOne.
    "fields": [ //optional
      ["{Field name}", "Field param",{"Field methods": "Field properties"}],
      ["src", ["nullable"]],
      ["bet", "float", 8, 2, {"default": "0.01", "cast": "string"}],
      ["href", {"cast": "Href"}], //If Cast is specified with a capital letter, scaffolding will consider it necessary to create a custom class for casting.
      "amount" //by default this field is: ['float', 8, 2]
    ],
    "relations": {
      //The name of the relationship can also be plural, in which case it will find or create a model.
      "{Relation name}{:Relation type name (optional)}": {
        //...new model data, or empty object if model exists
      },
      "tags:belongsToMany": {
        "const:title": "Tags",
        "fields": ["name", "active", "is_new", "mark_at"]
      },
      "rewards": {},
      "rewards": {
        //Relation configure api
        "uncascade": true // Switch off all cascade set
        "cascade_update": true // Switcher for cascade update
        "cascade_delete": true // Switcher for cascade delete
        "method": "custom_name" // The relation of method name
        "nullable": false // Set nullable relation / or use "?" before relation name
        "field": "reward_id" // The relation field name
        "related": "hasOne" // Specify the opposite relation manually 
      }
    },
    "rules": { // For create model request
      "name": "required|string|min:10|max:191", // Rules of model request
      "src": "nullable|string|Uppercase" // In order to create a custom rule, write the name of the class with a capital letter and it will be created.
    },
    "resource": [], //To create a resource {model_name}Resource
    "resource": ["AdminRules"], //To create multiple resources
    "factory": { // For generate of Fabrica
      "name": "\\faker.unique().name()",
      "src": "\\faker.imageUrl(640, 480, 'animals', true)",
      "time_at": "\\now()",
      "active": "\\rand(0,1)",
      "status": "wait",
    },
    "seed": "factory:30,50", // For add factory to seed you must add this string, for generating 30 to 50 records
    "seed": "factory:30", // To generate 30 records
    "seed": "factory", // To generate 1 record
  }
}

配置

发布配置

php artisan vendor:publish --tag=scaffold

所有属性都在 /config/scaffold.php 文件中发布的文件中进行了文档记录。

清除应用程序中生成的数据。

为了清除从应用程序中不断重新生成的数据,请运行命令 php artisan scaffold:clear

为了删除创建脚手架的所有文件,请在命令 php artisan scaffold:clear -a 中添加 -a--all 标志。

更灵活的 php api

Blanc添加

添加命名Blanc

\Bfg\Scaffold\ScaffoldConstruct::namedBlanc("user/location", [
    "location" => [
        "fields" => [
            'lat', 'lon'
        ]
    ],
    "user" => [
        "relations" => [
            "location" => []
        ]
    ]
]);

将Blanc添加到流中

\Bfg\Scaffold\ScaffoldConstruct::blanc([
    "location" => [
        "fields" => [
            'lat', 'lon'
        ]
    ],
    "user" => [
        "relations" => [
            "location" => []
        ]
    ]
]);

命令添加

添加命令

// Example of default command
\Bfg\Scaffold\ScaffoldConstruct::command(
    'required', 
    [\Bfg\Scaffold\ScaffoldCommands::class, 'required']
);