142 lines
4.8 KiB
PHP
142 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Room;
|
|
use Illuminate\Support\Facades\File; // Tambahkan ini di atas
|
|
|
|
class RoomController extends Controller
|
|
{
|
|
// Menampilkan daftar kamar
|
|
public function index(Request $request)
|
|
{
|
|
$search = $request->search;
|
|
|
|
$rooms = Room::when($search, function ($query, $search) {
|
|
return $query->where('room_number', 'like', "%{$search}%")
|
|
->orWhere('room_type', 'like', "%{$search}%")
|
|
->orWhere('status', 'like', "%{$search}%");
|
|
})
|
|
->paginate(10) // Batasi 10 data per halaman
|
|
->withQueryString(); // Agar query ?search tetap ada saat pindah halaman
|
|
|
|
return view('admin.rooms', compact('rooms'));
|
|
}
|
|
|
|
// Menampilkan form tambah kamar
|
|
public function create()
|
|
{
|
|
return view('admin.createroom');
|
|
}
|
|
|
|
// Menyimpan data kamar baru
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'room_number' => 'required|string|max:50|unique:rooms',
|
|
'room_type' => 'required|string|max:50',
|
|
'harga' => 'required|numeric|min:0',
|
|
'status' => 'required|in:tersedia,terisi,maintenance',
|
|
'lantai' => 'required|integer|min:1',
|
|
'fasilitas' => 'nullable|string',
|
|
'foto' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
|
'deskripsi' => 'nullable|string'
|
|
]);
|
|
|
|
// Cek apakah ada file foto yang diunggah
|
|
if ($request->hasFile('foto')) {
|
|
$foto = $request->file('foto');
|
|
$fotoName = time() . '_' . $foto->getClientOriginalName(); // Nama unik
|
|
$foto->move(public_path('assets/admin'), $fotoName); // Simpan ke folder
|
|
|
|
// Simpan hanya nama file (bukan full path)
|
|
$fotoPath = $fotoName;
|
|
} else {
|
|
$fotoPath = null;
|
|
}
|
|
|
|
Room::create([
|
|
'room_number' => $request->room_number,
|
|
'room_type' => $request->room_type,
|
|
'harga' => $request->harga,
|
|
'status' => $request->status,
|
|
'lantai' => $request->lantai,
|
|
'fasilitas' => $request->fasilitas,
|
|
'foto' => $fotoPath, // Simpan path foto di database
|
|
'deskripsi' => $request->deskripsi
|
|
]);
|
|
|
|
return redirect()->route('admin.rooms')->with('success', 'Kamar berhasil ditambahkan.');
|
|
}
|
|
|
|
// Menampilkan form edit kamar
|
|
public function edit($id_kamar)
|
|
{
|
|
$room = Room::findOrFail($id_kamar); // Pastikan parameter sama dengan yang dikirim di route
|
|
return view('admin.editroom', compact('room'));
|
|
}
|
|
|
|
// Memperbarui data kamar
|
|
public function update(Request $request, $id_kamar)
|
|
{
|
|
$request->validate([
|
|
'room_number' => 'required|string|max:50|unique:rooms,room_number,' . $id_kamar . ',id_kamar',
|
|
'room_type' => 'required|string|max:50',
|
|
'harga' => 'required|numeric|min:0',
|
|
'status' => 'required|in:tersedia,terisi,maintenance',
|
|
'lantai' => 'required|integer|min:1',
|
|
'fasilitas' => 'nullable|string',
|
|
'foto' => 'nullable|image|mimes:jpeg,png,jpg|max:2048', // Validasi file gambar
|
|
'deskripsi' => 'nullable|string'
|
|
]);
|
|
|
|
$room = Room::findOrFail($id_kamar);
|
|
|
|
// Cek apakah ada file foto yang diunggah
|
|
if ($request->hasFile('foto')) {
|
|
$foto = $request->file('foto');
|
|
$fotoName = time() . '_' . $foto->getClientOriginalName(); // Nama unik
|
|
$destinationPath = public_path('assets/admin');
|
|
|
|
// Hapus foto lama jika ada
|
|
if ($room->foto && File::exists(public_path('assets/admin/' . $room->foto))) {
|
|
File::delete(public_path('assets/admin/' . $room->foto));
|
|
}
|
|
|
|
// Simpan file baru ke folder
|
|
$foto->move($destinationPath, $fotoName);
|
|
|
|
// Simpan hanya nama file ke database
|
|
$room->foto = $fotoName;
|
|
}
|
|
|
|
$room->update($request->except('foto')); // Update semua kecuali foto
|
|
|
|
return redirect()->route('admin.rooms')->with('success', 'Kamar berhasil diperbarui.');
|
|
}
|
|
|
|
// Menghapus data kamar dan fotonya
|
|
public function destroy($id)
|
|
{
|
|
$room = Room::findOrFail($id);
|
|
|
|
// Hapus foto jika ada
|
|
if ($room->foto && file_exists(public_path($room->foto))) {
|
|
unlink(public_path($room->foto));
|
|
}
|
|
|
|
// Hapus data kamar
|
|
$room->delete();
|
|
|
|
return redirect()->route('admin.rooms')->with('success', 'Kamar berhasil dihapus.');
|
|
}
|
|
|
|
public function show($id_kamar)
|
|
{
|
|
$room = Room::findOrFail($id_kamar); // Ambil data kamar berdasarkan id
|
|
|
|
return view('users.detail_kamar', compact('room')); // Kirim ke view detail
|
|
}
|
|
}
|