add FotoRequest for improved validation and structure in FotoController
This commit is contained in:
parent
9898d3ecc9
commit
45969e0c77
|
|
@ -5,15 +5,11 @@
|
|||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Additional;
|
||||
use App\Models\PaketFoto;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Http\Requests\Admin\FotoRequest; // Gunakan Request baru
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class FotoController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$foto = PaketFoto::latest()->get();
|
||||
|
|
@ -21,100 +17,30 @@ public function index()
|
|||
return view('admin.paket-foto.index', compact('foto', 'additional'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
public function store(FotoRequest $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.',
|
||||
'foto.max' => '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');
|
||||
$data = $request->validated();
|
||||
if ($request->hasFile('foto')) {
|
||||
$file = $request->file('foto');
|
||||
$filename = time() . '_' . $file->getClientOriginalName();
|
||||
$path = $file->storeAs('img/foto', $filename, 'public');
|
||||
$data['foto'] = $path;
|
||||
}
|
||||
if (!$request->hasFile('foto')) {
|
||||
return redirect()->back()
|
||||
->with('error', 'File foto tidak ditemukan!')
|
||||
->withInput();
|
||||
}
|
||||
$file = $request->file('foto');
|
||||
$filename = time() . '_' . $file->getClientOriginalName();
|
||||
$path = $file->storeAs('img/foto', $filename, 'public');
|
||||
if (!$path) {
|
||||
return redirect()->back()
|
||||
->with('error', 'Gagal upload file!')
|
||||
->withInput();
|
||||
}
|
||||
PaketFoto::create([
|
||||
'nama' => $request->nama,
|
||||
'harga' => $request->harga,
|
||||
'durasi' => $request->durasi,
|
||||
'deskripsi' => $request->deskripsi,
|
||||
'foto' => $path,
|
||||
]);
|
||||
PaketFoto::create($data);
|
||||
return redirect()->back()->with('success', 'Paket foto baru berhasil ditambahkan!');
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id)
|
||||
public function update(FotoRequest $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.',
|
||||
'foto.max' => '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);
|
||||
}
|
||||
$data = $request->only(['nama', 'harga', 'durasi', 'deskripsi']);
|
||||
$data = $request->validated();
|
||||
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;
|
||||
$data['foto'] = $file->storeAs('img/foto', $filename, 'public');
|
||||
}
|
||||
$paket->update($data);
|
||||
return redirect()->back()->with('success', 'Paket foto berhasil diperbarui!');
|
||||
|
|
@ -122,14 +48,11 @@ public function update(Request $request, string $id)
|
|||
|
||||
public function destroy(string $id)
|
||||
{
|
||||
// Cari data berdasarkan primary key id_paket
|
||||
$paket = PaketFoto::where('id_paket', $id)->firstOrFail();
|
||||
|
||||
$paket = PaketFoto::findOrFail($id);
|
||||
if ($paket->foto) {
|
||||
Storage::disk('public')->delete($paket->foto);
|
||||
}
|
||||
$paket->delete();
|
||||
|
||||
return redirect()->back()->with('success', 'Paket foto dan filenya berhasil dihapus!');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
class FotoRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true; // Izinkan request
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'nama' => 'required|string|min:3|max:100',
|
||||
'harga' => 'required|numeric|min:0',
|
||||
'durasi' => 'required|integer|min:0',
|
||||
'deskripsi' => 'required|string|min:10',
|
||||
// Foto wajib saat Tambah (POST), opsional saat Edit (PUT)
|
||||
'foto' => $this->isMethod('post')
|
||||
? 'required|image|mimes:jpeg,png,jpg|max:2048'
|
||||
: 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'required' => 'Kolom :attribute wajib diisi.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'integer' => 'Input :attribute harus berupa angka valid.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
'min' => ':attribute minimal harus :min karakter/nilai.',
|
||||
'max' => ':attribute melebihi batas, maksimal :max karakter/KB.',
|
||||
'image' => ':attribute harus berupa file gambar (foto).',
|
||||
'mimes' => 'Format :attribute tidak didukung (jpeg, png, jpg).',
|
||||
'foto.max' => 'Ukuran :attribute maksimal 2MB.',
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'nama' => 'nama paket',
|
||||
'harga' => 'harga paket',
|
||||
'durasi' => 'durasi paket',
|
||||
'foto' => 'foto paket',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Penanganan khusus untuk Modal di Admin
|
||||
*/
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$redirect = redirect()->back()->withErrors($validator)->withInput();
|
||||
|
||||
if ($this->isMethod('post')) {
|
||||
// Trigger modal tambah foto
|
||||
$redirect->with('error_modal', 'createFoto');
|
||||
} else {
|
||||
// Trigger modal edit berdasarkan ID paket
|
||||
$redirect->with('error_id_foto', $this->route('foto'));
|
||||
}
|
||||
|
||||
throw new HttpResponseException($redirect);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue