如何从MySQL Dump生成Laravel迁移文件?附指定表建表语句
Convert MySQL Dump to Laravel Migration for
tb_edited_message Got it, let's turn that MySQL table definition into a clean, functional Laravel migration. Here's a step-by-step breakdown:
1. Generate the Migration File
First, run this Artisan command to create the base migration file:
php artisan make:migration create_tb_edited_message_table
2. Replace the Default Code with This
Open the generated file (in database/migrations/) and replace its content with the following code. This matches every detail of your MySQL schema:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('tb_edited_message', function (Blueprint $table) { // Matches bigint UNSIGNED AUTO_INCREMENT + primary key $table->bigIncrements('id')->comment('此条目的唯一标识符'); // Maps to bigint chat_id $table->bigInteger('chat_id')->comment('唯一聊天标识符'); // Matches bigint UNSIGNED message_id $table->unsignedBigInteger('message_id')->comment('唯一消息标识符'); // Nullable bigint user_id $table->bigInteger('user_id')->nullable()->comment('唯一用户标识符'); // Nullable timestamp with default null $table->timestamp('edit_date')->nullable()->default(null)->comment('消息编辑的时间戳格式日期'); // TEXT column for message content $table->text('text')->comment('针对文本消息,实际UTF-8编码内容'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('tb_edited_message'); } };
Key Notes About the Mapping
bigIncrements('id'): Laravel automatically sets this as the unsigned auto-incrementing primary key, which aligns with your MySQL definition.nullable(): Added touser_idandedit_dateto match their NULLable status in the original schema.unsignedBigInteger: Used formessage_idto replicate thebigint UNSIGNEDtype.- Comments: Preserved all original comments to keep the schema documentation intact.
3. Run the Migration
Once the file is saved, apply it to your database with:
php artisan migrate
内容的提问来源于stack exchange,提问作者Illia Yaremchuk
相关产品推荐
相关产品推荐

