定制 wecansync/laravel-migration-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wecansync/laravel-migration-generator

Composer 安装命令:

composer require wecansync/laravel-migration-generator

包简介

a package to generate migration schema for Models

README 文档

README

The simplest way to generate schema files like the Doctrine method used in Symfony.

Instead of manually writing migration content using the default Laravel command php artisan make:migration, which only creates the file and requires you to add columns manually, our script simplifies the process. By modifying the attribute values (like type) in your model, the script automatically detects the changes and generates the corresponding migration file for you. This eliminates the need for manual column type changes (e.g., from varchar to text or setting nullable) and streamlines the migration creation process.

Installation

composer require wecansync/laravel-migration-generator

Usage

1. Add the configuration attributes to your Model

    // App\Models\Category.php

    /**
     * Configuration for migration schema.
     *
     * @var array<string, array<string, mixed>>
     */
    public $migration_schema = [
        'name' => ['type' => 'string', 'length' => 255, 'nullable' => true],
    ];


    // App\Models\Brand.php

    /**
     * Configuration for migration schema.
     *
     * @var array<string, array<string, mixed>>
     */
    public $migration_schema = [
        'name' => ['type' => 'string', 'length' => 255, 'nullable' => true],
    ];


    // App\Models\Tag.php

    /**
     * Configuration for migration schema.
     *
     * @var array<string, array<string, mixed>>
     */
    public $migration_schema = [
        'name' => ['type' => 'string', 'length' => 255, 'nullable' => true],
    ];

    // App\Models\Product.php


    /**
     * Configuration for migration schema.
     *
     * @var array<string, array<string, mixed>>
     */
    public $migration_schema = [
        'name' => ['type' => 'string', 'length' => 255, 'nullable' => true],
        'description' => ['type' => 'text', 'nullable' => true],
        'price' => ['type' => 'integer', 'nullable' => true],
    ];

    // Define relationships in the model configuration
    public $relationships = [
        // first method (foreign): if you need to specify the foreign field column 
        [
            'column' => 'category_id',
            'type' => 'foreign',
            'model' => Category::class, // Related model
            'field' => 'id', // optional: Primary key in the related table (default is 'id')
        ],
        // second method (foreignId): use the id column
        [
            'column' => 'brand_id',
            'type' => 'foreignId',
            'model' => Brand::class, // Related model
        ],
        [
            'type' => 'manyToMany',
            'model' => Tag::class, // Related model
        ]
    ];

2. Generate migration files for your Model

php artisan generate:migration Category
php artisan generate:migration Brand
php artisan generate:migration Tag
php artisan generate:migration Product

3. Migrate

php artisan migrate

Default configuration

If you didn't add the configuration array to $migration_schema, the script will create the default migrations for your fields

public $migration_schema = [
// your fields here
'name' => [], // is the array is empty default_schema will be created
];

Important

Your $relationships and $migration_schema attributes should be public in order to be readable by the script.

public $migration_schema = [
// your fields here
];

public $relationships = [
// your relations here
];

统计信息

  • 总下载量: 1.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固