initial commit
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Traits\LogsActivity;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
@@ -18,13 +19,41 @@ class CategoryController extends Controller
|
||||
/**
|
||||
* Menampilkan daftar kategori.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
// Mengambil kategori beserta hitungan item di dalamnya (optional untuk insight)
|
||||
$categories = Category::latest()->get();
|
||||
return view('pages.inventory.categories.index', compact('categories'));
|
||||
public function index(Request $request): View
|
||||
{
|
||||
// 1. Proteksi Akses (Sesuai Matriks Role Management)
|
||||
Gate::authorize('check-access', ['categories', 'read']);
|
||||
|
||||
$query = Category::query();
|
||||
|
||||
// 2. SEARCH GLOBAL (Mencari di Nama & Keywords)
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->search;
|
||||
$query->where(function($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('keywords', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
// 3. LOGIKA SORTING DINAMIS
|
||||
$allowedSorts = ['name', 'created_at'];
|
||||
$sort = in_array($request->get('sort'), $allowedSorts) ? $request->get('sort') : 'name';
|
||||
$direction = strtolower($request->get('direction')) === 'desc' ? 'desc' : 'asc';
|
||||
|
||||
// 4. EKSEKUSI DENGAN PAGINASI (Standarisasi 15 item per halaman)
|
||||
$categories = $query->orderBy($sort, $direction)
|
||||
->paginate(15)
|
||||
->withQueryString();
|
||||
|
||||
// 5. STATISTIK RINGKAS
|
||||
$totalCategories = Category::count();
|
||||
$latestCategory = Category::latest()->first()?->name ?? '-';
|
||||
|
||||
return view('pages.inventory.categories.index', compact(
|
||||
'categories', 'totalCategories', 'latestCategory'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Menyimpan kategori baru.
|
||||
*/
|
||||
@@ -46,13 +75,12 @@ class CategoryController extends Controller
|
||||
* Menampilkan halaman edit (Hanya Admin).
|
||||
*/
|
||||
public function edit(Category $category): View
|
||||
{
|
||||
if (auth()->user()->role !== 'admin') {
|
||||
abort(403, 'Akses Ditolak.');
|
||||
}
|
||||
{
|
||||
|
||||
Gate::authorize('check-access', ['categories', 'update']);
|
||||
|
||||
return view('pages.inventory.categories.edit', compact('category'));
|
||||
}
|
||||
return view('pages.inventory.categories.edit', compact('category'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Memperbarui data kategori.
|
||||
|
||||
Reference in New Issue
Block a user