simedkom/app/Http/Controllers/Dashboard/LocationController.php
2025-10-13 08:35:36 +07:00

86 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\Http\Requests\LocationRequest;
use App\Models\Location;
class LocationController extends Controller
{
public function index()
{
return view('dashboard.locations.index', [
'title' => 'Data Lokus',
'new_data' => session()->get('new_data', null),
]);
}
public function create()
{
return view('dashboard.locations.create', [
'title' => 'Buat Lokus Baru',
]);
}
public function store(LocationRequest $request)
{
$validatedData = $request->validated();
try {
$createdLocation = Location::create($validatedData);
return redirect()->route('dashboard.locations.index')->with('success-status', 'Berhasil membuat lokus baru.')
->with('new_data', encrypt_id($createdLocation->id));
} catch (\Exception $e) {
return redirect()->back()->with('failed-status', 'Gagal menambahkan lokus.');
}
}
public function edit($id)
{
$location = Location::findOrFail(decrypt_id($id));
return view('dashboard.locations.edit', [
'title' => 'Ubah Lokus',
'location' => $location,
]);
}
public function update(LocationRequest $request, $id)
{
$validatedData = $request->validated();
try {
$location = Location::findOrFail(decrypt_id($id));
$location->update($validatedData);
return redirect()->route('dashboard.locations.show', ['id' => encrypt_id($location->id)])->with('success-status', 'Berhasil mengubah lokus.');
} catch (\Exception $e) {
return redirect()->back()->with('failed-status', 'Gagal mengubah lokus.');
}
}
public function show($id)
{
$location = Location::findOrFail(decrypt_id($id));
return view('dashboard.locations.show', [
'title' => 'Detail Lokus',
'location' => $location,
]);
}
public function destroy($id)
{
try {
$location = Location::findOrFail(decrypt_id($id));
$location->delete();
return response()->json(['message' => 'Berhasil menghapus lokus.']);
} catch (\Exception $e) {
return response()->json(['message' => 'Gagal menghapus lokus.'], 500);
}
}
}