validated(); if($validate){ try { City::create($validate); return redirect()->route('locations.cities.index')->with('success', 'City created successfully'); } catch (\Exception $e) { return redirect()->route('locations.cities.create')->with('error', 'Failed to create city'); } } } public function edit($id){ $city = City::find($id); $provinces = Province::all(); return view('location::cities.create', compact('city', 'provinces')); } public function update(CityRequest $request, $id){ $validate = $request->validated(); if($validate){ try { $city = City::find($id); $city->update($validate); return redirect()->route('locations.cities.index')->with('success', 'City updated successfully'); } catch (\Exception $e) { return redirect()->route('locations.cities.edit', $id)->with('error', 'Failed to update city'); } } } public function destroy($id){ try { City::destroy($id); echo json_encode(['message' => 'City deleted successfully', 'success' => true]); } catch (\Exception $e) { echo json_encode(['message' => 'Failed to delete city', 'success' => false]); } } public function dataForDatatables(Request $request){ if (is_null($this->user) || !$this->user->can('provinces.view')) { //abort(403, 'Sorry! You are not allowed to view users.'); } // Retrieve data from the database $query = City::query(); // Apply search filter if provided if ($request->has('search') && !empty($request->get('search'))) { $search = $request->get('search'); $search = explode('|', $search); if(isset($search[0]) &&!empty($search[0])){ $query->where('province_code',$search[0]); } $query->where(function ($q) use ($search) { $q->where('code', 'LIKE', "%$search[1]%"); $q->orWhere('name', 'LIKE', "%$search[1]%"); $q->orWhereRelation('province','name', 'LIKE', "%$search[1]%"); }); } // Apply sorting if provided if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) { $order = $request->get('sortOrder'); $column = $request->get('sortField'); $query->orderBy($column, $order); } // Get the total count of records $totalRecords = $query->count(); // Apply pagination if provided if ($request->has('page') && $request->has('size')) { $page = $request->get('page'); $size = $request->get('size'); $offset = ($page - 1) * $size; // Calculate the offset $query->skip($offset)->take($size); } // Get the filtered count of records $filteredRecords = $query->count(); // Get the data for the current page $data = $query->with('province')->get(); // Calculate the page count $pageCount = ceil($totalRecords/$request->get('size')); // Calculate the current page number $currentPage = 0 + 1; // Return the response data as a JSON object return response()->json([ 'draw' => $request->get('draw'), 'recordsTotal' => $totalRecords, 'recordsFiltered' => $filteredRecords, 'pageCount' => $pageCount, 'page' => $currentPage, 'totalCount' => $totalRecords, 'data' => $data, ]); } public function export() { return Excel::download(new CitiesExport, 'cities.xlsx'); } public function getCitiesByProvinceId($id){ return response()->json(City::where('province_code', $id)->get()); } }