Migration adalah salah satu fitur keren di Laravel yang memungkinkan kamu mengatur struktur database dengan kode PHP. Ini seperti version control untuk database kamu!
Apa itu Migration & Kenapa Penting?
Migration memungkinkan kamu untuk:
- Mendefinisikan struktur database dengan kode PHP
- Berbagi skema database dengan tim
- Melakukan rollback perubahan jika ada masalah
- Menjaga konsistensi database di berbagai environment
Setting Database di Laravel
1. Konfigurasi Database
File .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=
File config/database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', 'laravel_app'),
// ... konfigurasi lainnya
],
2. Membuat Database
# MySQL
mysql -u root -p
create database laravel_app;
# SQLite
touch database/database.sqlite
Perintah-perintah Migration
1. Membuat Migration
# Format dasar
php artisan make:migration create_users_table
# Dengan table creation
php artisan make:migration create_posts_table --create=posts
# Dengan table modification
php artisan make:migration add_status_to_users --table=users
2. Struktur Migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
3. Tipe Kolom yang Sering Dipakai
// Tipe data dasar
$table->string('name', 100);
$table->text('description');
$table->integer('price');
$table->decimal('amount', 8, 2);
$table->boolean('is_active');
$table->date('birth_date');
$table->datetime('published_at');
$table->json('metadata');
// Modifier
$table->string('email')->unique();
$table->integer('views')->default(0);
$table->string('slug')->nullable();
$table->timestamp('deleted_at')->nullable();
4. Menjalankan Migration
# Jalankan semua migration yang belum dijalankan
php artisan migrate
# Rollback migration terakhir
php artisan migrate:rollback
# Rollback semua migration
php artisan migrate:reset
# Refresh (rollback + migrate)
php artisan migrate:refresh
# Refresh dengan seeder
php artisan migrate:refresh --seed
Update Tabel Tanpa Hilang Data
1. Menambah Kolom Baru
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable()->after('email');
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('phone');
});
}
2. Mengubah Kolom yang Ada
// Pastikan sudah install doctrine/dbal
composer require doctrine/dbal
// Migration
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('name', 150)->change();
});
}
3. Foreign Key & Relationships
// Tambah foreign key
$table->foreignId('user_id')
->constrained()
->onDelete('cascade');
// Hapus foreign key
$table->dropForeign(['user_id']);
Tips & Best Practices
Penamaan Migration
- Gunakan nama yang deskriptif
- Tambahkan prefix tanggal (auto-generated)
- Contoh:
2024_03_15_create_products_table
Atomic Migration
- Satu migration = satu perubahan spesifik
- Jangan gabung banyak perubahan dalam satu file
Testing Migration
- Test di local dulu sebelum ke production
- Gunakan database testing terpisah
- Cek method
down()berfungsi dengan baik
Backup Data
- Selalu backup database production sebelum migrate
- Siapkan rollback plan
- Test di staging environment dulu
Kesimpulan
Migration adalah tool yang powerful untuk mengelola struktur database di Laravel. Dengan migration, kamu bisa:
- Tracking perubahan database
- Kolaborasi lebih mudah dengan tim
- Deployment yang lebih aman
- Maintenance database yang lebih terstruktur
Langkah Selanjutnya
- Pelajari Database Seeding untuk populate data testing
- Eksplorasi Query Builder dan Eloquent ORM
- Implementasikan foreign key constraints
- Buat migration untuk fitur-fitur kompleks
Jangan lupa follow @code.santuy untuk update tutorial Laravel lainnya! 🚀
#Laravel #PHP #Database #Migration #WebDevelopment