Undangan/backend/app/Http/Controllers/Api/PernikahanApiController.php

87 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Pelanggan;
use App\Models\PelangganDetail;
use App\Models\Template; // ✅ tambahkan ini
use Illuminate\Http\Request;
class PernikahanApiController extends Controller
{
public function store(Request $request)
{
$data = $request->validate([
'template_id' => 'required|exists:templates,id',
'nama_pemesan' => 'required|string|max:255',
'no_hp' => 'required|string|max:20',
'email' => 'required|email',
// Pria
'nama_lengkap_pria' => 'required|string|max:255',
'nama_panggilan_pria' => 'required|string|max:255',
'bapak_pria' => 'nullable|string|max:255',
'ibu_pria' => 'nullable|string|max:255',
'instagram_pria' => 'nullable|string',
'facebook_pria' => 'nullable|string',
'twitter_pria' => 'nullable|string',
// Wanita
'nama_lengkap_wanita' => 'required|string|max:255',
'nama_panggilan_wanita' => 'required|string|max:255',
'bapak_wanita' => 'nullable|string|max:255',
'ibu_wanita' => 'nullable|string|max:255',
'instagram_wanita' => 'nullable|string',
'facebook_wanita' => 'nullable|string',
'twitter_wanita' => 'nullable|string',
// Cerita
'cerita_kita' => 'nullable|string',
// Akad
'hari_tanggal_akad' => 'nullable|date',
'waktu_akad' => 'nullable|string',
'alamat_akad' => 'nullable|string',
'maps_akad' => 'nullable|string',
// Resepsi
'hari_tanggal_resepsi' => 'nullable|date',
'waktu_resepsi' => 'nullable|string',
'alamat_resepsi' => 'nullable|string',
'maps_resepsi' => 'nullable|string',
// Tambahan
'no_rekening1' => 'nullable|string',
'no_rekening2' => 'nullable|string',
'link_musik' => 'nullable|string',
'galeri' => 'nullable|string',
]);
// ✅ Ambil template berdasarkan template_id
$template = Template::with('kategori')->findOrFail($data['template_id']);
// ✅ Simpan ke tabel pelanggan
$pelanggan = Pelanggan::create([
'nama_pemesan' => $data['nama_pemesan'],
'nama_template' => $template->nama_template,
'kategori' => $template->kategori->nama ?? '-',
'email' => $data['email'],
'no_tlpn' => $data['no_hp'],
'harga' => $template->harga,
]);
// ✅ Simpan detail form ke tabel pelanggan_details
PelangganDetail::create([
'pelanggan_id' => $pelanggan->id,
'detail_form' => $data,
]);
return response()->json([
'success' => true,
'message' => 'Form pernikahan berhasil dikirim',
'data' => $pelanggan->load('details')
], 201);
}
}