php artisan make:migration add_column_name_to_table_name_table --table=table_name

เปลี่ยน add_column_name_to_table_name_table เป็นชื่อที่สื่อความหมายถึงการเปลี่ยนแปลง

เปลี่ยน table_name เป็นชื่อตารางที่คุณต้องการแก้ไข

ตัวอย่าง:

ถ้าคุณต้องการเพิ่มคอลัมน์ status ลงในตาราง users:
php artisan make:migration add_status_to_users_table --table=users

แก้ไขไฟล์ Migration

หลังจากรันคำสั่ง จะมีไฟล์ถูกสร้างใน database/migrations/ ให้แก้ไขเนื้อหา:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('status')->default('active'); // เพิ่มคอลัมน์ใหม่
    });
}

รัน Migration

php artisan migrate

แท็ก: none

ปิดความเห็นแล้ว