add BuketRequest for improved validation and structure in BuketController
This commit is contained in:
parent
39ba315f0a
commit
ff929d6905
|
|
@ -4,8 +4,7 @@
|
|||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Buket;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Http\Requests\Admin\BuketRequest; // Panggil Request baru kamu
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class BuketController extends Controller
|
||||
|
|
@ -16,104 +15,29 @@ public function index()
|
|||
return view('admin.produk-buket.index', compact('buket'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
public function store(BuketRequest $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => 'required|string|min:3|max:100',
|
||||
'ukuran' => 'required|in:S,M,L',
|
||||
'kategori' => 'required|in:single,fresh,premium_fresh,artificial',
|
||||
'harga' => 'required|numeric|min:0',
|
||||
'request_khusus' => 'nullable|string|max:255',
|
||||
'deskripsi' => 'required|string|min:10',
|
||||
'foto' => 'required|image|mimes:jpeg,png,jpg|max:2048',
|
||||
], [
|
||||
'required' => 'Kolom :attribute tidak boleh kosong.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'min' => ':attribute terlalu pendek, minimal harus :min karakter.',
|
||||
'max' => ':attribute melebihi batas, maksimal :max karakter/KB.',
|
||||
'in' => 'Pilihan :attribute tidak sesuai dengan data yang tersedia.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
'image' => ':attribute harus berupa file gambar (foto).',
|
||||
'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.',
|
||||
'foto.max' => 'Ukuran :attribute terlalu besar, maksimal adalah 2MB.',
|
||||
], [
|
||||
'nama' => 'nama buket',
|
||||
'ukuran' => 'ukuran buket',
|
||||
'kategori' => 'kategori buket',
|
||||
'harga' => 'harga',
|
||||
'request_khusus' => 'request khusus',
|
||||
'deskripsi' => 'deskripsi produk',
|
||||
'foto' => 'foto produk',
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return redirect()->back()
|
||||
->withErrors($validator)
|
||||
->withInput()
|
||||
->with('error_modal', 'create');
|
||||
}
|
||||
$path = null;
|
||||
$data = $request->validated();
|
||||
if ($request->hasFile('foto')) {
|
||||
$file = $request->file('foto');
|
||||
$filename = time() . '_' . $file->getClientOriginalName();
|
||||
$path = $file->storeAs('img/buket', $filename, 'public');
|
||||
$data['foto'] = $file->storeAs('img/buket', $filename, 'public');
|
||||
}
|
||||
Buket::create([
|
||||
'nama' => $request->nama,
|
||||
'ukuran' => $request->ukuran,
|
||||
'kategori' => $request->kategori,
|
||||
'harga' => $request->harga,
|
||||
'request_khusus' => $request->request_khusus,
|
||||
'deskripsi' => $request->deskripsi,
|
||||
'foto' => $path,
|
||||
]);
|
||||
Buket::create($data);
|
||||
return redirect()->back()->with('success', 'Produk buket berhasil ditambahkan!');
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id)
|
||||
public function update(BuketRequest $request, string $id)
|
||||
{
|
||||
$buket = Buket::findOrFail($id);
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => 'required|string|min:3|max:100',
|
||||
'ukuran' => 'required|in:S,M,L',
|
||||
'kategori' => 'required|in:single,fresh,premium_fresh,artificial',
|
||||
'harga' => 'required|numeric|min:0',
|
||||
'request_khusus' => 'nullable|string|max:255',
|
||||
'deskripsi' => 'required|string|min:10',
|
||||
'foto' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
||||
], [
|
||||
'required' => 'Kolom :attribute tidak boleh kosong.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'min' => ':attribute terlalu pendek, minimal harus :min karakter.',
|
||||
'max' => ':attribute melebihi batas, maksimal :max karakter/KB.',
|
||||
'in' => 'Pilihan :attribute tidak sesuai dengan data yang tersedia.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
'image' => ':attribute harus berupa file gambar (foto).',
|
||||
'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.',
|
||||
'foto.max' => 'Ukuran :attribute terlalu besar, maksimal adalah 2MB.',
|
||||
], [
|
||||
'nama' => 'nama buket',
|
||||
'ukuran' => 'ukuran buket',
|
||||
'kategori' => 'kategori buket',
|
||||
'harga' => 'harga',
|
||||
'request_khusus' => 'request khusus',
|
||||
'deskripsi' => 'deskripsi produk',
|
||||
'foto' => 'foto produk',
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return redirect()->back()
|
||||
->withErrors($validator)
|
||||
->withInput()
|
||||
->with('error_id', $id);
|
||||
}
|
||||
$data = $request->only(['nama', 'ukuran', 'kategori', 'harga', 'request_khusus', 'deskripsi']);
|
||||
$data = $request->validated();
|
||||
if ($request->hasFile('foto')) {
|
||||
if ($buket->foto) {
|
||||
Storage::disk('public')->delete($buket->foto);
|
||||
}
|
||||
$file = $request->file('foto');
|
||||
$filename = time() . '_' . $file->getClientOriginalName();
|
||||
$path = $file->storeAs('img/buket', $filename, 'public');
|
||||
$data['foto'] = $path;
|
||||
$data['foto'] = $file->storeAs('img/buket', $filename, 'public');
|
||||
}
|
||||
$buket->update($data);
|
||||
return redirect()->back()->with('success', 'Produk buket berhasil diperbarui!');
|
||||
|
|
@ -122,12 +46,10 @@ public function update(Request $request, string $id)
|
|||
public function destroy(string $id)
|
||||
{
|
||||
$buket = Buket::findOrFail($id);
|
||||
|
||||
if ($buket->foto) {
|
||||
Storage::disk('public')->delete($buket->foto);
|
||||
}
|
||||
$buket->delete();
|
||||
|
||||
return redirect()->back()->with('success', 'Produk dan foto berhasil dihapus permanen!');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
class BuketRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
// Ubah jadi true supaya request diizinkan
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'nama' => 'required|string|min:3|max:100',
|
||||
'ukuran' => 'required|in:S,M,L',
|
||||
'kategori' => 'required|in:single,fresh,premium_fresh,artificial',
|
||||
'harga' => 'required|numeric|min:0',
|
||||
'request_khusus' => 'nullable|string|max:255',
|
||||
'deskripsi' => 'required|string|min:10',
|
||||
// Dinamis: required jika store (POST), nullable jika update (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 tidak boleh kosong.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'min' => ':attribute terlalu pendek, minimal harus :min karakter.',
|
||||
'max' => ':attribute melebihi batas, maksimal :max karakter/KB.',
|
||||
'in' => 'Pilihan :attribute tidak sesuai dengan data yang tersedia.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
'image' => ':attribute harus berupa file gambar (foto).',
|
||||
'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.',
|
||||
'foto.max' => 'Ukuran :attribute terlalu besar, maksimal adalah 2MB.',
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'nama' => 'nama buket',
|
||||
'ukuran' => 'ukuran buket',
|
||||
'kategori' => 'kategori buket',
|
||||
'harga' => 'harga',
|
||||
'request_khusus' => 'request khusus',
|
||||
'deskripsi' => 'deskripsi produk',
|
||||
'foto' => 'foto produk',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Trik untuk Modal: Mengembalikan error ke modal yang tepat
|
||||
*/
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$redirect = redirect()->back()->withErrors($validator)->withInput();
|
||||
|
||||
if ($this->isMethod('post')) {
|
||||
$redirect->with('error_modal', 'create');
|
||||
} else {
|
||||
// Mengambil ID dari parameter route untuk modal edit
|
||||
$redirect->with('error_id', $this->route('buket'));
|
||||
}
|
||||
|
||||
throw new HttpResponseException($redirect);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue