定制 noouh/migration-to-model 二次开发

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

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

noouh/migration-to-model

最新稳定版本:1

Composer 安装命令:

composer require noouh/migration-to-model

包简介

A Laravel package to convert migrations into models

README 文档

README

Migration to Model is a Laravel package that converts your database migrations into Eloquent models. It generates fillable properties, table names, and relationships based on your migration files.

Installation

You can install the package via composer:

composer require noouh/migration-to-model

Usage

To convert a specific migration file into a model, you can use the following artisan command:

php artisan migrations:convert {migration}

Replace {migration} with the name of your migration file (without the timestamp). For example, if your migration file is 2024_07_23_000000_create_users_table.php, you would use:

php artisan migrations:convert create_users_table

To convert all migration files into models, simply run:

php artisan migrations:convert

This command will generate model files in the app/Models directory with the appropriate fillable properties, table names, and relationship methods based on detected foreign keys.

Example

Suppose you have the following migration file:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->foreignId('profile_id')->constrained();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Running the command:

php artisan migrations:convert create_users_table

Will generate the following model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasFactory;

    protected $table = 'users';
    protected $fillable = ['name', 'email', 'profile_id'];

    public function profile()
    {
        return $this->belongsTo(Profile::class, 'profile_id', 'id');
    }
}

License

The Migration to Model package is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-07-23