skl/app/Http/Controllers/KategoriController.php

66 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\KategoriRequest;
use App\Models\Kategori;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;
class KategoriController extends Controller
{
public function index()
{
try {
$kategori = Kategori::latest()->get();
return Inertia::render('admin/kategori/index_kategori', ['kategori' => $kategori]);
} catch (\Exception $e) {
Log::error('Error fetching Kategori: ' . $e->getMessage());
return back()->with('error', 'Something went wrong.');
}
}
public function store(KategoriRequest $request)
{
try {
$kategori = Kategori::withTrashed()
->where('NamaKategori', $request->NamaKategori)
->first();
if ($kategori) {
$kategori->restore();
return redirect()->route('admin.kategori.index')->with('success', 'Kategori berhasil dikembalikan.');
}
Kategori::create($request->validated());
return redirect()->route('admin.kategori.index')->with('success', 'Kategori berhasil dibuat.');
} catch (\Exception $e) {
Log::error('Error creating Kategori: ' . $e->getMessage());
return back()->with('error', 'Something went wrong.');
}
}
public function update(KategoriRequest $request, Kategori $kategori)
{
try {
$kategori->update($request->validated());
return redirect()->route('admin.kategori.index')->with('success', 'Kategori berhasil diperbarui.');
} catch (\Exception $e) {
Log::error('Error updating Kategori: ' . $e->getMessage());
return back()->with('error', 'Something went wrong.');
}
}
public function destroy(Kategori $kategori)
{
try {
$kategori->delete();
return redirect()->route('admin.kategori.index')->with('success', 'Kategori berhasil dihapus.');
} catch (\Exception $e) {
Log::error('Error deleting Kategori: ' . $e->getMessage());
return back()->with('error', 'Something went wrong.');
}
}
}