isAgentPortal() ? collect() : User::query()->orderBy('username')->get(['id', 'username']); return view('admin.notes.index', [ 'portal' => $this->portal(), 'agents' => $agents, ]); } public function data(Request $request) { $q = $this->baseQuery($request); $sortable = ['id', 'created_at', 'updated_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('notes.'.$field, $order); $limit = max(1, min(100, (int) $request->query('limit', 15))); $page = max(1, (int) $request->query('page', 1)); $paginator = $q->paginate($limit, ['*'], 'page', $page); $portal = $this->portal(); $data = collect($paginator->items())->map(function ($row) use ($portal) { $content = $row->content; if (is_array($content)) { $content = json_encode($content, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); } return [ 'id' => $row->id, 'device_key' => $row->device_key ?: '', 'channel_id' => $row->device_channel_id ?: '', 'content' => $content ?: '', 'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'), 'updated_at' => optional($row->updated_at)->format('Y-m-d H:i:s'), 'detail_url' => route($portal.'.devices.show', ['device' => $row->device_id, 'tab' => 'notes']), ]; })->values(); return response()->json([ 'code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $data, ]); } private function baseQuery(Request $request): Builder { $q = Note::query() ->join('devices', 'devices.id', '=', 'notes.device_id') ->select([ 'notes.*', 'devices.device_id as device_key', 'devices.channel_id as device_channel_id', ]); AgentScope::applyDeviceChannelScope($q, $this->agent()); $channelId = trim((string) $request->query('channel_id', '')); $deviceKey = trim((string) $request->query('device_key', '')); $agentUserId = (int) $request->query('agent_user_id', 0); if ($channelId !== '') { $q->where('devices.channel_id', 'like', '%'.$channelId.'%'); } if ($deviceKey !== '') { $q->where('devices.device_id', 'like', '%'.$deviceKey.'%'); } if (! $this->isAgentPortal() && $agentUserId > 0) { AgentScope::applyAgentUserFilter($q, $agentUserId); } return $q; } }