142 lines
5.2 KiB
PHP
142 lines
5.2 KiB
PHP
<?php
|
|
|
|
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\Storage;
|
|
|
|
class FotoController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$foto = PaketFoto::latest()->get();
|
|
$additional = Additional::latest()->get();
|
|
|
|
// Kirim keduanya ke view paket-foto.index
|
|
return view('admin.paket-foto.index', compact('foto', 'additional'));
|
|
}
|
|
|
|
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:0',
|
|
'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();
|
|
$path = $file->storeAs('img/foto', $filename, 'public');
|
|
}
|
|
|
|
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!');
|
|
}
|
|
|
|
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:0',
|
|
'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_foto', $id); // Membuka modal edit yang error otomatis
|
|
}
|
|
|
|
$data = $request->only(['nama', 'harga', 'durasi', 'deskripsi']);
|
|
|
|
if ($request->hasFile('foto')) {
|
|
if ($paket->foto) {
|
|
Storage::disk('public')->delete($paket->foto);
|
|
}
|
|
|
|
$file = $request->file('foto');
|
|
$filename = time() . '_' . $file->getClientOriginalName();
|
|
$path = $file->storeAs('img/foto', $filename, 'public');
|
|
$data['foto'] = $path;
|
|
}
|
|
|
|
$paket->update($data);
|
|
|
|
return redirect()->back()->with('success', 'Paket foto berhasil diperbarui!');
|
|
}
|
|
|
|
public function destroy(string $id)
|
|
{
|
|
// Cari data berdasarkan primary key id_paket
|
|
$paket = PaketFoto::where('id_paket', $id)->firstOrFail();
|
|
|
|
if ($paket->foto) {
|
|
Storage::disk('public')->delete($paket->foto);
|
|
}
|
|
$paket->delete();
|
|
|
|
return redirect()->back()->with('success', 'Paket foto dan filenya berhasil dihapus!');
|
|
}
|
|
}
|