noouh/auto-model

A Laravel包,用于从表定义自动生成模型和迁移。

v1.0.2 2024-07-03 09:21 UTC

This package is auto-updated.

Last update: 2024-09-22 10:49:26 UTC


README

A Laravel包,用于从表定义自动生成模型和迁移。

关于公司

Noouh For Integrated Solutions 致力于提供创新的软件解决方案。我们专注于开发高品质的应用程序,以满足客户的具体需求。

安装

要安装Noouh Auto Model包,您可以使用Composer要求它

composer require noouh/auto-model

发布存根

安装包后,使用以下命令发布存根

php artisan vendor:publish --tag=stubs

这将发布model.stubmigration.stub文件到您项目的stubs目录。

用法

创建一个包含您的表定义和关系的JSON文件(例如,tables.json)。以下是示例结构

[
  {
    "table": "users",
    "columns": [
      { "name": "name", "type": "string" },
      { "name": "email", "type": "string" }
    ]
  },
  {
    "table": "posts",
    "columns": [
      { "name": "title", "type": "string" },
      { "name": "content", "type": "text" },
      { "name": "user_id", "type": "unsignedBigInteger" }
    ],
    "relationships": [
      { "type": "belongsTo", "relatedTable": "users", "foreignKey": "user_id" }
    ]
  }
]

运行命令以生成模型和迁移

php artisan generate:models /path/to/tables.json

示例

给定以下tables.json文件

[
  {
    "table": "users",
    "columns": [
      { "name": "name", "type": "string" },
      { "name": "email", "type": "string" }
    ]
  },
  {
    "table": "posts",
    "columns": [
      { "name": "title", "type": "string" },
      { "name": "content", "type": "text" },
      { "name": "user_id", "type": "unsignedBigInteger" }
    ],
    "relationships": [
      { "type": "belongsTo", "relatedTable": "users", "foreignKey": "user_id" }
    ]
  }
]

运行命令

php artisan generate:models /path/to/tables.json

将生成

  • app/Models/User.php中创建一个User模型
  • app/Models/Post.php中创建一个Post模型,它与User模型有belongsTo关系
  • users表创建迁移文件
  • posts表创建迁移文件,该表具有对users表的引用键

许可证

此软件包是开源软件,根据MIT许可证许可。