has('search') && !empty($request->get('search'))) { $search = $request->get('search'); $query->where(function ($q) use ($search) { $q->where('crdno', 'LIKE', "%$search%") ->orWhere('accflag', 'LIKE', "%$search%") ->orWhere('cracc1', 'LIKE', "%$search%") ->orWhere('cracc2', 'LIKE', "%$search%") ->orWhere('cracc3', 'LIKE', "%$search%") ->orWhere('cracc4', 'LIKE', "%$search%") ->orWhere('cracc5', 'LIKE', "%$search%") ->orWhere('craccnam1', 'LIKE', "%$search%") ->orWhere('craccnam2', 'LIKE', "%$search%") ->orWhere('craccnam3', 'LIKE', "%$search%") ->orWhere('craccnam4', 'LIKE', "%$search%") ->orWhere('craccnam5', 'LIKE', "%$search%") ->orWhere('crsts', 'LIKE', "%$search%") ->orWhere('cttype', 'LIKE', "%$search%") ->orWhere('ctdesc', 'LIKE', "%$search%") ->orWhere('crdate', 'LIKE', "%$search%") ->orWhere('branch', 'LIKE', "%$search%") ->orWhere('fee', 'LIKE', "%$search%") ->orWhere('currency', 'LIKE', "%$search%"); }); } // 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->get(); // Calculate the page count $pageCount = ceil($filteredRecords / ($request->get('size') ?: 1)); // Calculate the current page number $currentPage = $request->get('page') ?: 1; $data = $data->map(function ($item) { $item->fee = $item->biaya?->biaya; return $item; }); // 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, ]); } }