authorize('audit.view_any'); $request->validate([ 'via' => ['sometimes', Rule::in(['panel', 'api', 'token', 'web', 'console'])], 'from' => ['sometimes', 'date'], 'to' => ['sometimes', 'date'], ]); $entries = AuditLog::query() ->when($request->filled('user_id'), fn ($q) => $q->where('user_id', $request->integer('user_id'))) ->when($request->filled('action'), function ($q) use ($request): void { $action = (string) $request->string('action'); str_ends_with($action, '.') ? $q->where('action', 'like', $action.'%') : $q->where('action', $action); }) ->when($request->filled('subject_type'), fn ($q) => $q->where('subject_type', $request->string('subject_type'))) ->when($request->filled('subject_id'), fn ($q) => $q->where('subject_id', (string) $request->string('subject_id'))) ->when($request->filled('via'), fn ($q) => $q->where('via', $request->string('via'))) ->when($request->filled('from'), fn ($q) => $q->where('created_at', '>=', $request->date('from'))) ->when($request->filled('to'), fn ($q) => $q->where('created_at', '<=', $request->date('to'))) ->latest('id') ->paginate(perPage: min($request->integer('per_page', 50), 100)) ->withQueryString(); return AuditLogResource::collection($entries); } }