orderBy('id', 'desc'); if ($request->has("search") &&!empty($request->get("search"))) { $query = $query->where("name", "LIKE", "%{$request->get("search")}%") ->orWhere("nomor", "LIKE", "%{$request->get("search")}%"); } return SpatialPlanningsResource::collection($query->paginate()); } public function store(SpatialPlanningsRequest $request) { try{ $validated = $request->validated(); $data = SpatialPlanning::create($validated); return response()->json(['message' => 'Successfully created', new SpatialPlanningsResource($data)]); }catch(\Exception $e){ return response()->json([ 'message' => $e->getMessage() ], 500); } } /** * Display the specified resource. */ public function show(string $id) { // } /** * Update the specified resource in storage. */ public function update(SpatialPlanningsRequest $request, string $id) { try{ $validated = $request->validated(); $data = SpatialPlanning::find($id); $data->update($validated); return response()->json(['message' => 'Successfully updated', new SpatialPlanningsResource($data)]); }catch(\Exception $e){ return response()->json([ 'message' => $e->getMessage() ], 500); } } /** * Remove the specified resource from storage. */ public function destroy(string $id) { try{ SpatialPlanning::destroy($id); return response()->json([ 'message' => 'Successfully deleted' ], 200); }catch(\Exception $e){ return response()->json([ 'message' => $e->getMessage() ], 500); } } public function upload(ExcelUploadRequest $request){ try{ if(!$request->hasFile('file')){ return response()->json([ 'error' => 'No file provided' ], 400); } $file = $request->file('file'); Excel::import(new SpatialPlanningImport, $file); return response()->json([ 'message' => 'Successfully imported' ], 200); }catch(\Exception $e){ return response()->json([ 'error' => $e->getMessage() ], 500); } } }