public function getIndex() { $wallets = Cache::tags('user' . Auth::user()->id)->remember('wallets', Config::get('cache.ttl'), function () { return WalletsAddress::selectRaw('wallets.*, ifnull(wallets_addresses.balance, 0) as balance, (select sum(amount) from wallets_transactions where wallets_transactions.user_id = wallets_addresses.user_id and wallets_transactions.wallet_id = wallets.id and confirmed = 0) as pending, (select sum(amount) from wallets_withdrawals where wallets_withdrawals.user_id = wallets_addresses.user_id and wallets_withdrawals.wallet_id = wallets.id and wallets_transaction_id is null) as withdrawal_amount, ifnull((select sum(amount) from orders where orders.want_wallet_id = wallets_addresses.wallet_id and status = "open" and type = "sell"), 0) as orders_amount_sell, ifnull((select sum(amount*price) from orders where orders.wallet_id = wallets_addresses.wallet_id and status = "open" and type = "buy"), 0) as orders_amount_buy')->whereNull('user_id')->orWhere('user_id', Auth::user()->id)->rightJoin('wallets', 'wallets.id', '=', 'wallets_addresses.wallet_id')->get(); }); return view('user.wallets.index', compact(['wallets'])); }
/** * Get user balances. */ public function getIndex() { $response = ['success' => false]; $error = false; if (!Auth::check()) { $response['success'] = false; $response['error'] = 'auth error'; $error = true; } if (!$error) { $wallets = Cache::tags('user' . Auth::user()->id)->remember('wallets', Config::get('cache.ttl'), function () { return WalletsAddress::selectRaw('wallets.short, ifnull(wallets_addresses.balance, 0) as balance, (select sum(amount) from orders where orders.want_wallet_id = wallets_addresses.wallet_id and status = "open" and type = "sell") as orders')->whereNull('user_id')->orWhere('user_id', Auth::user()->id)->rightJoin('wallets', 'wallets.id', '=', 'wallets_addresses.wallet_id')->get(); }); foreach ($wallets as $wallet) { $response['wallets'][$wallet->short] = ['balance' => (double) $wallet->balance, 'orders_balance' => (double) $wallet->orders]; } $response['success'] = true; } return response()->json($response); }