noouh / migration-to-model
此包最新版本(1)没有提供许可证信息。
Laravel包,用于将迁移文件转换为模型
1
2024-07-23 07:39 UTC
Requires
- php: >=7.3
- illuminate/support: ^8.0|^9.0|^10.0
README
Migration to Model 是一个 Laravel 包,可以将您的数据库迁移文件转换为 Eloquent 模型。它根据您的迁移文件生成填充属性、表名和关系。
安装
您可以通过 composer 安装此包
composer require noouh/migration-to-model
用法
要将特定的迁移文件转换为模型,可以使用以下 artisan 命令
php artisan migrations:convert {migration}
将 {migration}
替换为您迁移文件的名字(不带时间戳)。例如,如果您的迁移文件是 2024_07_23_000000_create_users_table.php
,您将使用
php artisan migrations:convert create_users_table
要转换所有迁移文件为模型,只需运行
php artisan migrations:convert
此命令将在 app/Models
目录中生成模型文件,具有适当的填充属性、表名和基于检测到的外键的关系方法。
示例
假设您有以下迁移文件
<?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'); } }
运行命令
php artisan migrations:convert create_users_table
将生成以下模型
<?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'); } }
许可证
Migration to Model 包是开源软件,许可协议为 MIT 协议。