get('limit', $request->get('per_page', 20)), 200); $query = Mail::query()->orderByDesc('created_at'); // sort=field_asc|field_desc if ($sort = $request->get('sort')) { $parts = explode('_', $sort); $dir = array_pop($parts); $col = implode('_', $parts); $allowed = ['created_at', 'subject', 'status', 'to']; if (in_array($col, $allowed, true) && in_array($dir, ['asc', 'desc'], true)) { $query->reorder($col, $dir); } } if ($status = $request->get('status')) { $query->where('status', $status); } if ($customerId = $request->get('customer_id')) { $query->where('customer_id', (int) $customerId); } if ($search = trim((string) $request->get('search', ''))) { $query->where(function ($q) use ($search) { $q->where('to', 'like', "%{$search}%") ->orWhere('subject', 'like', "%{$search}%"); }); } $paginator = $query->paginate($limit); return response()->json([ 'error' => false, 'data' => [ 'items' => $paginator->items(), 'total' => $paginator->total(), 'per_page' => $paginator->perPage(), 'current_page' => $paginator->currentPage(), 'last_page' => $paginator->lastPage(), ], ]); } public function show(int $id): JsonResponse { $log = Mail::find($id); if (!$log) { return response()->json(['error' => true, 'message' => 'Không tìm thấy email log'], 404); } return response()->json(['error' => false, 'data' => $log]); } }