MIF_E31222629/app/Http/Controllers/MobilController.php

128 lines
4.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\MobilModels as Mobil;
use App\Models\SubKriteriaModels as SubKriteria;
use Illuminate\Http\Request;
class MobilController extends Controller
{
public function index(Request $request)
{
$query = Mobil::with('subkriteria');
// Cek apakah ada parameter pencarian
if ($request->has('search') && $request->search != '') {
$search = $request->search;
$query->where('nama_mobil', 'like', '%' . $search . '%')
->orWhere('kelengkapan_mobil', 'like', '%' . $search . '%')
->orWhere('harga_sewa_per_hari', 'like', '%' . $search . '%')
->orWhere('tahun', 'like', '%' . $search . '%');
}
$mobils = $query->paginate(5);
return view('mobil.index', compact('mobils'));
}
public function create()
{
$subkriteria = SubKriteria::all();
return view('mobil.form', compact('subkriteria'));
}
public function store(Request $request)
{
$request->validate([
'nama_mobil' => 'required|string|max:255',
'sub_kriteria_id' => 'required|exists:sub_kriteria,id',
'gambar' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
'kelengkapan_mobil' => 'nullable|string',
'harga_sewa_per_hari' => 'nullable|string|max:255',
'konsumsi_bbm' => 'nullable|numeric|min:0',
'tahun' => 'nullable|integer|min:1900|max:' . date('Y'),
]);
$mobil = new Mobil();
$mobil->nama_mobil = $request->nama_mobil;
$mobil->sub_kriteria_id = $request->sub_kriteria_id;
$mobil->kelengkapan_mobil = $request->kelengkapan_mobil;
$mobil->harga_sewa_per_hari = $request->harga_sewa_per_hari;
$mobil->konsumsi_bbm = $request->konsumsi_bbm;
$mobil->tahun = $request->tahun;
if ($request->hasFile('gambar')) {
$file = $request->file('gambar');
$originalName = $file->getClientOriginalName();
$cleanName = preg_replace('/[^A-Za-z0-9\-_\.]/', '_', $originalName);
$filename = time() . '_' . $cleanName;
$file->move(public_path('assets/images'), $filename);
$mobil->gambar = 'assets/images/' . $filename;
}
$mobil->save();
return redirect()->route('mobil.index')->with('success', 'Data mobil berhasil ditambahkan!');
}
public function edit(Mobil $mobil)
{
$subkriteria = SubKriteria::all();
return view('mobil.form', compact('mobil', 'subkriteria'));
}
public function update(Request $request, Mobil $mobil)
{
$request->validate([
'nama_mobil' => 'required|string|max:255',
'sub_kriteria_id' => 'required|exists:sub_kriteria,id',
'gambar' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
'kelengkapan_mobil' => 'nullable|string',
'harga_sewa_per_hari' => 'nullable|string|max:255',
'konsumsi_bbm' => 'nullable|numeric|min:0',
'tahun' => 'nullable|integer|min:1900|max:' . date('Y'),
]);
$mobil->nama_mobil = $request->nama_mobil;
$mobil->sub_kriteria_id = $request->sub_kriteria_id;
$mobil->kelengkapan_mobil = $request->kelengkapan_mobil;
$mobil->harga_sewa_per_hari = $request->harga_sewa_per_hari;
$mobil->konsumsi_bbm = $request->konsumsi_bbm;
$mobil->tahun = $request->tahun;
if ($request->hasFile('gambar')) {
// Hapus gambar lama jika ada
if ($mobil->gambar && file_exists(public_path($mobil->gambar))) {
unlink(public_path($mobil->gambar));
}
$file = $request->file('gambar');
$originalName = $file->getClientOriginalName();
$cleanName = preg_replace('/[^A-Za-z0-9\-_\.]/', '_', $originalName);
$filename = time() . '_' . $cleanName;
$file->move(public_path('assets/images'), $filename);
$mobil->gambar = 'assets/images/' . $filename;
}
$mobil->save();
return redirect()->route('mobil.index')->with('success', 'Data mobil berhasil diupdate!');
}
public function destroy(Mobil $mobil)
{
if ($mobil->gambar && file_exists(public_path($mobil->gambar))) {
unlink(public_path($mobil->gambar));
}
$mobil->delete();
return redirect()->route('mobil.index')->with('success', 'Data mobil berhasil dihapus!');
}
}