233 lines
4.5 KiB
Markdown
233 lines
4.5 KiB
Markdown
# 🔗 Setup Laravel dengan Supabase PostgreSQL
|
|
|
|
## Step 1: Ambil Credentials dari Supabase Dashboard
|
|
|
|
1. Login ke [Supabase Dashboard](https://app.supabase.com)
|
|
2. Pilih project Anda
|
|
3. Klik **Settings** → **Database**
|
|
4. Scroll down ke bagian **Connection string**
|
|
5. Pilih "URI" tab
|
|
6. Copy connection string (format: `postgresql://...`)
|
|
|
|
## Step 2: Parse Connection String
|
|
|
|
Connection string format Supabase:
|
|
```
|
|
postgresql://[user]:[password]@[host]:[port]/[database]
|
|
```
|
|
|
|
Contoh:
|
|
```
|
|
postgresql://postgres.ohahxydutfqlfuxrjdwm:HG!rji9a9wnLaZY@aws-1-ap-northeast-1.pooler.supabase.com:5432/postgres
|
|
```
|
|
|
|
Parse menjadi:
|
|
- **User**: `postgres.ohahxydutfqlfuxrjdwm`
|
|
- **Password**: `HG!rji9a9wnLaZY`
|
|
- **Host**: `aws-1-ap-northeast-1.pooler.supabase.com`
|
|
- **Port**: `5432`
|
|
- **Database**: `postgres`
|
|
|
|
## Step 3: Update .env File
|
|
|
|
```env
|
|
DB_CONNECTION=pgsql
|
|
DB_HOST=aws-1-ap-northeast-1.pooler.supabase.com
|
|
DB_PORT=5432
|
|
DB_DATABASE=postgres
|
|
DB_USERNAME=postgres.ohahxydutfqlfuxrjdwm
|
|
DB_PASSWORD=HG!rji9a9wnLaZY
|
|
```
|
|
|
|
## Step 4: Clear Config Cache
|
|
|
|
```bash
|
|
php artisan config:cache
|
|
php artisan config:clear
|
|
```
|
|
|
|
## Step 5: Test Connection
|
|
|
|
Run migrations:
|
|
```bash
|
|
php artisan migrate
|
|
```
|
|
|
|
Atau test di Tinker:
|
|
```bash
|
|
php artisan tinker
|
|
DB::connection()->getPdo()
|
|
```
|
|
|
|
---
|
|
|
|
## ⚙️ Laravel Configuration Files
|
|
|
|
File yang handle database connection:
|
|
|
|
### `config/database.php`
|
|
```php
|
|
'pgsql' => [
|
|
'driver' => 'pgsql',
|
|
'host' => env('DB_HOST', 'localhost'),
|
|
'port' => env('DB_PORT', '5432'),
|
|
'database' => env('DB_DATABASE', 'forge'),
|
|
'username' => env('DB_USERNAME', 'forge'),
|
|
'password' => env('DB_PASSWORD', ''),
|
|
// ...
|
|
],
|
|
```
|
|
|
|
❗ **PENTING**: Jangan edit `config/database.php` - edit `.env` saja!
|
|
|
|
---
|
|
|
|
## 🔐 Security Notes
|
|
|
|
⚠️ **JANGAN COMMIT `.env` ke Git!**
|
|
|
|
`.env` file Anda berisi credentials sensitif. Pastikan:
|
|
|
|
1. `.env` ada di `.gitignore`
|
|
2. Share `.env.example` saja ke team
|
|
3. Setiap developer punya `.env` sendiri
|
|
|
|
`.gitignore` harus ada:
|
|
```
|
|
.env
|
|
.env.local
|
|
.env.*.local
|
|
```
|
|
|
|
---
|
|
|
|
## 🧪 Testing Database Connection
|
|
|
|
### Method 1: Direct Test
|
|
```bash
|
|
php artisan tinker
|
|
```
|
|
|
|
Kemudian:
|
|
```php
|
|
DB::select('select 1')
|
|
// Jika tidak error, connection berhasil!
|
|
```
|
|
|
|
### Method 2: Run Query
|
|
```php
|
|
App\Models\UserDosen::count()
|
|
// Harusnya return jumlah users
|
|
```
|
|
|
|
### Method 3: Check Environment
|
|
```bash
|
|
php artisan tinker
|
|
env('DB_HOST')
|
|
env('DB_USERNAME')
|
|
```
|
|
|
|
---
|
|
|
|
## 🔄 Controllers & Routes
|
|
|
|
**Kabar Baik**: TIDAK ADA PERUBAHAN DIPERLUKAN!
|
|
|
|
Karena Supabase adalah PostgreSQL, semua Eloquent queries bekerja sama:
|
|
|
|
```php
|
|
// Ini work di Supabase (sama dengan DB lokal)
|
|
UserDosen::where('nip', $nip)->first()
|
|
UserStaff::paginate(10)
|
|
Attendance::whereBetween('tanggal', [$start, $end])->get()
|
|
```
|
|
|
|
Laravel Eloquent sudah abstrak semua database operations.
|
|
|
|
---
|
|
|
|
## 📝 Troubleshooting
|
|
|
|
### Error: "SQLSTATE[08006] could not connect to server"
|
|
|
|
**Solution:**
|
|
- Pastikan credentials benar
|
|
- Cek IP address whitelist di Supabase (Settings → Database → Connection pooler)
|
|
- Cek firewall settings
|
|
|
|
### Error: "column does not exist"
|
|
|
|
**Solution:**
|
|
- Jalankan migrations: `php artisan migrate`
|
|
- Cek schema di Supabase console
|
|
|
|
### Error: "FATAL: database does not exist"
|
|
|
|
**Solution:**
|
|
- Verify DB_DATABASE name benar
|
|
- Di Supabase biasanya "postgres"
|
|
|
|
### Slow Queries
|
|
|
|
**Solution:**
|
|
- Supabase memiliki connection pooler limits
|
|
- Gunakan connection pooling dengan PgBouncer mode
|
|
- Update `DB_HOST` ke pooler URL
|
|
|
|
---
|
|
|
|
## 🎯 Current Setup Status
|
|
|
|
Berdasarkan `.env` Anda sekarang:
|
|
|
|
```
|
|
✅ DB_CONNECTION = pgsql
|
|
✅ DB_HOST = aws-1-ap-northeast-1.pooler.supabase.com
|
|
✅ DB_PORT = 5432
|
|
✅ DB_DATABASE = postgres
|
|
✅ DB_USERNAME = postgres.ohahxydutfqlfuxrjdwm
|
|
✅ DB_PASSWORD = [terisi]
|
|
```
|
|
|
|
**Status: READY TO USE** ✓
|
|
|
|
---
|
|
|
|
## 📊 Database Schema
|
|
|
|
Schema yang sudah ada:
|
|
|
|
```
|
|
✅ users_dosen
|
|
├── id (uuid)
|
|
├── nama, nip, nidn
|
|
├── prodi, foto, role
|
|
├── password, remember_token
|
|
└── timestamps
|
|
|
|
✅ users_staff
|
|
├── id (uuid)
|
|
├── nama, nip, nidn
|
|
├── bagian, foto, role
|
|
├── password, remember_token
|
|
└── timestamps
|
|
|
|
✅ attendances
|
|
├── id (uuid)
|
|
├── user_id, user_type
|
|
├── tanggal, jam_masuk, jam_keluar
|
|
├── durasi_jam, keterangan
|
|
└── timestamps
|
|
```
|
|
|
|
---
|
|
|
|
## ✅ Next Steps
|
|
|
|
1. Verify connection: `php artisan tinker` → `DB::select('select 1')`
|
|
2. Run migrations jika belum: `php artisan migrate`
|
|
3. Test login dengan credentials existing
|
|
4. Check data di Supabase dashboard
|
|
|
|
Semuanya sudah configured! 🚀
|