Spaces:
Sleeping
Sleeping
| namespace App\Http\Controllers\Admin; | |
| use App\Http\Controllers\Controller; | |
| use App\Models\Category; | |
| use Illuminate\Http\Request; | |
| class CategoryController extends Controller | |
| { | |
| public function index() | |
| { | |
| $categories = Category::withCount('events')->orderBy('name')->paginate(20); | |
| return view('admin.categories.index', compact('categories')); | |
| } | |
| public function store(Request $request) | |
| { | |
| $request->validate(['name' => 'required|string|max:100|unique:categories,name']); | |
| Category::create(['name' => $request->name]); | |
| return back()->with('success', 'Kategori berhasil ditambahkan.'); | |
| } | |
| public function update(Request $request, Category $category) | |
| { | |
| $request->validate(['name' => 'required|string|max:100|unique:categories,name,' . $category->id]); | |
| $category->update(['name' => $request->name]); | |
| return back()->with('success', 'Kategori berhasil diperbarui.'); | |
| } | |
| public function destroy(Category $category) | |
| { | |
| if ($category->events()->exists()) { | |
| return back()->with('error', 'Kategori tidak bisa dihapus karena masih digunakan oleh event.'); | |
| } | |
| $category->delete(); | |
| return back()->with('success', 'Kategori berhasil dihapus.'); | |
| } | |
| } | |