From f9eb2397af67a966d36344ffeec5429269c930d0 Mon Sep 17 00:00:00 2001 From: LailaWulandarii Date: Sat, 27 Dec 2025 18:15:35 +0700 Subject: [PATCH] add logic in foto controller and paket foto section for crud paket foto --- app/Http/Controllers/admin/FotoController.php | 157 ++++++++++---- .../views/admin/paket-foto/index.blade.php | 196 ++++++++++++++---- .../partials/modal-create-foto.blade.php | 135 ++++++------ .../partials/modal-delete-foto.blade.php | 40 +--- .../partials/modal-edit-foto.blade.php | 95 ++++----- .../partials/modal-show-foto.blade.php | 87 ++++---- 6 files changed, 422 insertions(+), 288 deletions(-) diff --git a/app/Http/Controllers/admin/FotoController.php b/app/Http/Controllers/admin/FotoController.php index 4620edf..3a107c8 100644 --- a/app/Http/Controllers/admin/FotoController.php +++ b/app/Http/Controllers/admin/FotoController.php @@ -3,7 +3,12 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; +use App\Models\Additional; +use App\Models\PaketFoto; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Facades\File; + class FotoController extends Controller { @@ -12,54 +17,132 @@ class FotoController extends Controller */ public function index() { - return view('admin.paket-foto'); + $foto = PaketFoto::latest()->get(); + $additional = Additional::latest()->get(); + + // Kirim keduanya ke view paket-foto.index + return view('admin.paket-foto.index', compact('foto', 'additional')); } - /** - * Show the form for creating a new resource. - */ - public function create() - { - // - } - - /** - * Store a newly created resource in storage. - */ public function store(Request $request) { - // + $validator = Validator::make($request->all(), [ + 'nama' => 'required|string|min:3|max:100', + 'harga' => 'required|numeric|min:0', + 'durasi' => 'required|integer|min:1', + 'deskripsi' => 'required|string|min:10', + 'foto' => 'required|image|mimes:jpeg,png,jpg|max:2048', + ], [ + 'required' => 'Kolom :attribute wajib diisi.', + 'string' => 'Input :attribute harus berupa teks valid.', + 'integer' => 'Input :attribute harus berupa angka valid.', + 'max' => ':attribute melebihi batas, maksimal :max karakter/KB.', + 'image' => ':attribute harus berupa file gambar (foto).', + 'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.', + 'max.file' => 'Ukuran :attribute maksimal 2MB.', + 'min' => ':attribute minimal harus :min karakter/nilai.', + 'numeric' => ':attribute harus berupa angka.', + + ], [ + 'nama' => 'nama paket', + 'harga' => 'harga paket', + 'durasi' => 'durasi paket', + 'foto' => 'foto paket', + ]); + + if ($validator->fails()) { + return redirect()->back() + ->withErrors($validator) + ->withInput() + ->with('error_modal', 'createFoto'); // Agar modal terbuka otomatis saat error + } + + $path = null; + if ($request->hasFile('foto')) { + $file = $request->file('foto'); + $filename = time() . '_' . $file->getClientOriginalName(); + // Simpan langsung ke folder public untuk asset() + $file->move(public_path('img/foto'), $filename); + $path = 'img/foto/' . $filename; + } + + PaketFoto::create([ + 'nama' => $request->nama, + 'harga' => $request->harga, + 'durasi' => $request->durasi, + 'deskripsi' => $request->deskripsi, + 'foto' => $path, + ]); + + return redirect()->back()->with('success', 'Paket foto baru berhasil ditambahkan!'); } - /** - * Display the specified resource. - */ - public function show(string $id) - { - // - } - - /** - * Show the form for editing the specified resource. - */ - public function edit(string $id) - { - // - } - - /** - * Update the specified resource in storage. - */ public function update(Request $request, string $id) { - // + $paket = PaketFoto::findOrFail($id); + + $validator = Validator::make($request->all(), [ + 'nama' => 'required|string|min:3|max:100', + 'harga' => 'required|numeric|min:0', + 'durasi' => 'required|integer|min:1', + 'deskripsi' => 'required|string|min:10', + 'foto' => 'nullable|image|mimes:jpeg,png,jpg|max:2048', + ], [ + 'required' => 'Kolom :attribute wajib diisi.', + 'string' => 'Input :attribute harus berupa teks valid.', + 'integer' => 'Input :attribute harus berupa angka valid.', + 'max' => ':attribute melebihi batas, maksimal :max karakter/KB.', + 'image' => ':attribute harus berupa file gambar (foto).', + 'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.', + 'max.file' => 'Ukuran :attribute maksimal 2MB.', + 'min' => ':attribute minimal harus :min karakter/nilai.', + 'numeric' => ':attribute harus berupa angka.', + ], [ + 'nama' => 'nama paket', + 'harga' => 'harga paket', + 'durasi' => 'durasi paket', + 'foto' => 'foto paket', + ]); + + if ($validator->fails()) { + return redirect()->back() + ->withErrors($validator) + ->withInput() + ->with('error_id', $id); // Membuka modal edit yang error otomatis + } + + $data = $request->only(['nama', 'harga', 'durasi', 'deskripsi']); + + if ($request->hasFile('foto')) { + // Hapus file lama jika ada foto baru yang diunggah + if ($paket->foto && File::exists(public_path($paket->foto))) { + File::delete(public_path($paket->foto)); + } + + $file = $request->file('foto'); + $filename = time() . '_' . $file->getClientOriginalName(); + $file->move(public_path('img/foto'), $filename); + $data['foto'] = 'img/foto/' . $filename; + } + + $paket->update($data); + + return redirect()->back()->with('success', 'Paket foto berhasil diperbarui!'); } - /** - * Remove the specified resource from storage. - */ public function destroy(string $id) { - // + // Cari data berdasarkan primary key id_paket + $paket = PaketFoto::where('id_paket', $id)->firstOrFail(); + + // 1. Cek dan hapus file foto dari folder public + if ($paket->foto && File::exists(public_path($paket->foto))) { + File::delete(public_path($paket->foto)); + } + + // 2. Hapus data dari database + $paket->delete(); + + return redirect()->back()->with('success', 'Paket foto dan filenya berhasil dihapus!'); } } diff --git a/resources/views/admin/paket-foto/index.blade.php b/resources/views/admin/paket-foto/index.blade.php index 054bc74..29602ed 100644 --- a/resources/views/admin/paket-foto/index.blade.php +++ b/resources/views/admin/paket-foto/index.blade.php @@ -3,6 +3,21 @@ @section('title', 'Paket Foto') @section('content') + {{-- ALERT SUKSES --}} + @if (session('success')) + + @endif + + {{-- ALERT ERROR UMUM (Jika ada error selain validasi modal) --}} + @if (session('error')) + + @endif
@@ -39,36 +54,54 @@ - - - - - - + + + + + + - - - - - - - - + @forelse ($foto as $f) + + + + + + + + + @include('admin.paket-foto.partials.modal-show-foto') + @include('admin.paket-foto.partials.modal-edit-foto') + @include('admin.paket-foto.partials.modal-delete-foto') + @empty + + + + @endforelse
No.Nama PaketDeskripsiHargaFotoAksiNo.Nama PaketDeskripsiHargaFotoAksi
Graidenvehicula.aliquet@semconsequat.co.uk076 4820 8838OffenburgOffenburg - - - - - - - - - -
{{ $loop->iteration }}{{ $f->nama }}{{ Str::limit($f->deskripsi, 50) }}Rp {{ number_format($f->harga, 0, ',', '.') }} + Foto Produk + + + + + + + + + + + + +
+
+ Belum ada data paket foto. +
+
@@ -77,28 +110,44 @@ - - - - + + + + - - - - - - + @forelse ($additional as $ad) + + + + + + + {{-- @include('admin.produk-buket.partials.modal-show') + @include('admin.produk-buket.partials.modal-edit') + @include('admin.produk-buket.partials.modal-delete') --}} + @empty + + + + @endforelse
No.Nama AdditionalHargaAksiNo.Nama AdditionalHargaAksi
Graidenvehicula.aliquet@semconsequat.co.uk076 4820 8838 - - - - - - -
{{ $loop->iteration }}{{ $ad->nama }}Rp {{ number_format($ad->harga, 0, ',', '.') }} + + + + + + + +
+
+ Belum ada data additional. +
+
@@ -115,5 +164,60 @@ @include('admin.paket-foto.partials.modal-create-additional') @include('admin.paket-foto.partials.modal-edit-additional') @include('admin.paket-foto.partials.modal-delete-additional') + @push('scripts') + + @endpush @endsection diff --git a/resources/views/admin/paket-foto/partials/modal-create-foto.blade.php b/resources/views/admin/paket-foto/partials/modal-create-foto.blade.php index f2986de..ee11b62 100644 --- a/resources/views/admin/paket-foto/partials/modal-create-foto.blade.php +++ b/resources/views/admin/paket-foto/partials/modal-create-foto.blade.php @@ -1,32 +1,40 @@