get('size') ?: 10; $master = Kecamatan::with('kabupatenKota') ->when($request->search, function ($q, $search) { $search = strtolower($search); $q->where(function ($query) use ($search) { $query->whereRaw('lower(kode) like ?', ["%{$search}%"]) ->orWhereRaw('lower(nama) like ?', ["%{$search}%"]); }); }) ->when($request->kabupaten_kota_id, fn($q,$v) => $q->where('kabupaten_kota_id',$v)) ->when($request->provinsi_id, fn ($q, $v) => $q->whereHas('kabupatenKota.provinsi', fn ($k) => $k->where('provinsi_id', $v) ) ); if ($request->filled('sort')) { $dir = str_starts_with($request->sort, '-') ? 'desc' : 'asc'; $column = ltrim($request->sort, '-'); $allowed = ['id', 'kode', 'nama']; if (in_array($column, $allowed)) { $master->orderBy($column, $dir); } } else { $master->orderBy('kode', 'asc'); } $masterList = $master->paginate($size); return KecamatanResource::collection($masterList); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { // } /** * Display the specified resource. */ public function show(string $id): JsonResponse { return response()->json(KecamatanResource::make(Kecamatan::with('kabupatenKota')->findOrFail($id))); } /** * Show the form for editing the specified resource. */ public function edit(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } }