panodream / migbuilder
Laravel 模型、数据填充器、工厂与迁移 MySQL 反向工程工具
dev-main
2023-01-05 10:01 UTC
Requires
- php: ^7.3|^8.0
This package is auto-updated.
Last update: 2024-09-05 13:43:34 UTC
README
MigBuilder 是一个将 MySQL 数据库反向工程到模型、工厂、数据填充器和迁移文件的工具,用于 Laravel Eloquent ORM。
它读取数据库结构并尽可能地生成代码,以简化 Laravel 中的模型生成。
除非模型结构完全符合预期,否则生成的代码文件将包含不准确的内容。因此,建议在生成代码后始终检查代码。
“完美”的表定义如下
- 表应包含一个 单个列主键,最好其名称为 'id'
- 关系必须声明,并且始终由一个单列组成
- 尚未测试不常见的数据类型(二进制、长文本等)
安装
composer require pangodream/migbuilder
使用
php artisan migbuilder originschema
Migbuilder 将读取 originschema 数据库(originschema 是 Laravel 配置文件 app.php 中的连接名称)并为每个表生成迁移、模型、数据填充器和工厂文件。在 Laravel 配置文件 database.php 中声明两个不同的连接是一种好的做法:
- 一个指向现有数据库的备用连接,用于反向工程
- 主连接,将用于部署迁移文件和使用模型
注意:如果您使用上述命令并且 任何 生成的文件已经存在,则除非您指定 --overwrite 参数,否则生成过程将不会开始。请注意,使用覆盖将会破坏任何与同名文件相同的文件中的信息。
注意:Migbuilder 不是一个无错误的代码生成器,而是一个节省手动编写代码行的助手,因此您应该始终审查生成的代码。
示例生成的文件
源DDL
CREATE TABLE product ( id INT(11) NOT NULL, name VARCHAR(45) NOT NULL DEFAULT '', slug VARCHAR(45) NOT NULL DEFAULT '', description VARCHAR(45) NOT NULL DEFAULT '', price VARCHAR(45) NOT NULL DEFAULT '', subcategory_id INT(11) NOT NULL DEFAULT '0', brand_id INT(11) NOT NULL DEFAULT '0', quantity VARCHAR(45) NOT NULL DEFAULT '0', PRIMARY KEY (id), INDEX FK_products_subcategories (subcategory_id), INDEX FK_products_brands (brand_id), CONSTRAINT FK_products_brands FOREIGN KEY (brand_id) REFERENCES brand (id), CONSTRAINT FK_products_subcategories FOREIGN KEY (subcategory_id) REFERENCES subcategory (id) ) COLLATE='utf8_general_ci' ENGINE=InnoDB;
迁移文件
<?php /* Generated automatically using MigBuilder by Pangodream */ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateProductTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('product', function (Blueprint $table) { $table->id('id'); $table->string('name', 45)->default(''); $table->string('slug', 45)->default(''); $table->string('description', 45)->default(''); $table->string('price', 45)->default(''); $table->unsignedBigInteger('subcategory_id')->default(0); $table->unsignedBigInteger('brand_id')->default(0); $table->string('quantity', 45)->default('0'); $table->timestamps(); // Indexes $table->index('id'); // Constraints & Foreign Keys $table->foreign('subcategory_id')->references('id')->on('subcategory'); $table->foreign('brand_id')->references('id')->on('brand'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::disableForeignKeyConstraints(); Schema::dropIfExists('product'); Schema::enableForeignKeyConstraints(); } }
模型文件
<?php /* Generated automatically using MigBuilder by Pangodream */ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; protected $table = 'product'; // Fillables (remove the columns you don't need) protected $fillable = ['id', 'name', 'slug', 'description', 'price', 'subcategory_id', 'brand_id', 'quantity', ]; // Parent relationships (change belongsTo to belongsToMany or similar if needed) public function Subcategory(){ return $this->belongsTo(Subcategory::class); } public function Brand(){ return $this->belongsTo(Brand::class); } // Child relationships (change hasMany to hasOne or similar if needed) public function ColorProduct(){ return $this->hasMany(ColorProduct::class); } public function Size(){ return $this->hasMany(Size::class); } }
数据填充器文件
<?php /* Generated automatically using MigBuilder by Pangodream */ namespace Database\Seeders; use Illuminate\Database\Seeder; use App\Models\Product; class ProductSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // Record sample structure $product = [ //'id' => , //'name' => '', //'slug' => '', //'description' => '', //'price' => '', //'subcategory_id' => , //'brand_id' => , //'quantity' => '', ]; } }
工厂文件
<?php /* Generated automatically using MigBuilder by Pangodream */ namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; use App\Models\Product; class ProductFactory extends Factory { protected $model = Product::class; /** * Define the model's default state. * * @return array */ public function definition() { return [ // ]; } }