Skip to content
Tutorial emka
Menu
  • Home
  • Debian Linux
  • Ubuntu Linux
  • Red Hat Linux
Menu

Cara Mengganti nama kolom dengan Laravel Migrate

Posted on February 23, 2024

Dalam Laravel, migrasi database menyediakan cara yang nyaman untuk mengelola perubahan skema database, termasuk merubah nama kolom. Mengganti nama kolom adalah tugas umum dalam pemeliharaan database, dan Laravel menyederhanakan proses ini dengan menggunakan alat migrasi bawaannya. Pada artikel ini, kita akan menjelajahi panduan langkah demi langkah tentang cara mengganti nama kolom di Laravel.

Langkah 1: Membuat Migrasi

Langkah pertama adalah membuat file migrasi menggunakan perintah Artisan make:migration. Buka terminal atau command prompt Anda, arahkan ke direktori proyek Laravel, dan jalankan perintah berikut:

php artisan make:migration rename_column_in_table

Gantilah rename_column_in_table dengan nama deskriptif untuk migrasi Anda.

Langkah 2: Mendefinisikan Migrasi

Buka file migrasi yang baru saja dibuat yang terletak di direktori database/migrations. Di dalam metode up(), gunakan metode table() yang disediakan oleh pembuat skema Laravel untuk mendefinisikan perubahan pada skema database Anda. Berikut contoh cara mengganti nama kolom:

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class RenameColumnInTable extends Migration
{
    /**
     * Jalankan migrasi.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('your_table_name', function (Blueprint $table) {
            $table->renameColumn('old_column_name', 'new_column_name');
        });
    }

    /**
     * Mundurkan migrasi.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('your_table_name', function (Blueprint $table) {
            $table->renameColumn('new_column_name', 'old_column_name');
        });
    }
}

Gantilah ‘your_table_name’, ‘old_column_name’, dan ‘new_column_name’ dengan nama tabel dan nama kolom yang sesuai.

Langkah 3: Menjalankan Migrasi

Setelah mendefinisikan migrasi, simpan file tersebut dan kembali ke terminal atau command prompt Anda. Jalankan perintah berikut untuk menjalankan migrasi:

php artisan migrate

Perintah ini akan menjalankan metode up() dari migrasi Anda, mengganti nama kolom yang ditentukan dalam tabel database Anda.

Langkah 4: Rollback (Opsional)

Jika Anda perlu membatalkan perubahan yang dilakukan oleh migrasi, Anda dapat membatalkan migrasi dengan perintah berikut:

php artisan migrate:rollback

Perintah ini akan menjalankan metode down() dari migrasi Anda, mengembalikan perubahan nama kolom.

Mengganti nama kolom di Laravel adalah proses yang mudah berkat sistem migrasi yang kuat. Dengan mengikuti langkah-langkah yang diuraikan dalam panduan ini, Anda dapat mengelola perubahan skema database secara efisien dan menjaga struktur database aplikasi Laravel Anda tetap terorganisir dan terkini.

Recent Posts

  • How to build a high-performance private photo cloud with Immich and TrueNAS SCALE
  • How to Build an Endgame Local AI Agent Setup Using an 8-Node NVIDIA Cluster with 1TB Memory
  • How to Master Windows Event Logs to Level Up Your Cybersecurity Investigations and SOC Career
  • How to Build Ultra-Resilient Databases with Amazon Aurora Global Database and RDS Proxy for Maximum Uptime and Performance
  • How to Build Real-Time Personalization Systems Using AWS Agentic AI to Make Every User Feel Special
  • How to Transform Your Windows 11 Interface into a Sleek and Modern Aesthetic Masterpiece
  • How to Understand Google’s New TPU 8 Series for Massive AI Training and Inference
  • How to Level Up Your PC Gaming Experience with the New Valve Steam Controller and Its Advanced Features
  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to Clear Copilot Memory in Windows 11 Step by Step
  • How to Show Battery Percentage on Windows 11
  • How to Fix VMSp Service Failed to Start on Windows 10/11
  • How to Fix Taskbar Icon Order in Windows 11/10
  • How to Disable Personalized Ads in Copilot on Windows 11
  • What is the Microsoft Teams Error “We Couldn’t Connect the Call” Error?
  • Why Does the VirtualBox System Service Terminate Unexpectedly? Here is the Full Definition
  • Why is Your Laptop Touchpad Overheating? Here are the Causes and Fixes
  • How to Disable All AI Features in Chrome Using Windows 11 Registry
  • How to Avoid Problematic Windows Updates: A Guide to System Stability
  • What is Microsoft Visual C++ Redistributable and How to Fix Common Errors?
  • What is the 99% Deletion Bug? Understanding and Fixing Windows 11 File Errors
  • Daftar Sekarang! Beasiswa S2 di Italia dari IYT Scholarship 2026 Sudah Dibuka
  • Sejarah Hantavirus dan Perkembangannya Sampai ke Indonesia
  • Kementerian Pendidikan: Mapel Bahasa Inggris Wajib di SD Mulai 2027!
  • Ketua Fraksi PKB MPR-RI: Kemenag Respon Cepat Pendidikan Santri Ndolo Kusumo Pati yang Terdampak
  • Viral Video Sejoli Di Balai Kota Panggul Trenggalek, Satpol PP Janji Usut
  • Testing Baidu Ernie 5.1, ultra-efficient thinking mode to solve your most complex coding and reasoning challenges with ease
  • How to Evaluate AI Logic Performance Using DeepSeek V4 Flash Think and Gemini 3.1 Flash Light in Complex Reasoning Tests
  • How to Build Your Own Content Factory Using the New Google NotebookLM Intelligence Updates
  • How to Use DFlash for Blazing Fast AI Text Generation on Gemma 4 26B
  • How to Optimize Your AI Agent Using Compiled Knowledge Layers to Replace Traditional RAG Systems
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025
©2026 Tutorial emka | Design: Newspaperly WordPress Theme