SystemLog::actionLabels(), ]); } public function data(Request $request) { $q = SystemLog::query(); $username = trim((string) $request->query('username', '')); $action = trim((string) $request->query('action', '')); $keyword = trim((string) $request->query('keyword', '')); if ($username !== '') { $q->where('actor_username', 'like', '%'.$username.'%'); } if ($action !== '' && isset(SystemLog::actionLabels()[$action])) { $q->where('action', $action); } if ($keyword !== '') { $q->where('content', 'like', '%'.$keyword.'%'); } $sortable = ['id', 'created_at']; $field = (string) $request->query('field', 'id'); $order = strtolower((string) $request->query('order', 'desc')) === 'asc' ? 'asc' : 'desc'; if (! in_array($field, $sortable, true)) { $field = 'id'; } $q->orderBy($field, $order); $limit = max(1, min(100, (int) $request->query('limit', 20))); $page = max(1, (int) $request->query('page', 1)); $paginator = $q->paginate($limit, ['*'], 'page', $page); $data = collect($paginator->items())->map(function (SystemLog $row) { return [ 'id' => $row->id, 'actor_role' => $row->actorRoleLabel(), 'actor_username' => $row->actor_username ?: '—', 'content' => $row->content ?: '', 'ip' => $row->ip ?: '', 'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'), ]; })->values(); return response()->json([ 'code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $data, ]); } }