Динамический запрос Laravel

Приходит массив вида

array:3 [▼
  0 => "1, 2"
  1 => "2, 1"
  2 => "3, 3"
]

в переменную $account_type_select, и если там что то есть нужно по каждому значению достать объект. Мой код вытягивает только первое значение, а нужны все

 $sub_join_account_type = AccountType::select('id')
        ->whereColumn('account_type_id', 'id')
        ->getQuery();

    $sub_join_currency_id = AccountType::select('currency_id')
        ->whereColumn('account_type_id', 'id')
        ->getQuery();

    $sub_join_currency = Currency::select('name')
        ->whereColumn('currency_id', 'id')
        ->getQuery();

    $accounts = Account::select(
        'accounts.*',
        DB::raw("
            (
                (
                    SELECT IFNULL(SUM(amount),0) 
                    FROM transactions 
                    WHERE dr_cr = 'cr' AND status = 'complete' AND account_id = accounts.id
                ) - 
                (
                    SELECT IFNULL(SUM(amount),0) 
                    FROM transactions 
                    WHERE dr_cr = 'dr' AND status != 'reject' AND account_id = accounts.id
                )
           ) as balance"
        )
    )
        ->selectSub($sub_join_account_type, 'account_type_sub')
        ->selectSub($sub_join_currency_id, 'currency_id')
        ->selectSub($sub_join_currency, 'currency')
        ->when($user_type, function ($query, $user_type) {
            if ($user_type == 'staff') {
                return $query->where('accounts.created_by', Auth::id());
            }
        })
        ->when($min_balance, function ($query, $min_balance) {
            return $query->having('balance', '>', $min_balance);
        })
        ->when($max_balance, function ($query, $max_balance) {
            return $query->having('balance', '<', $max_balance);
        })
        ->when($status_value !== 'all', function ($query) use ($status_value) {
            return $query->where('accounts.status', '=', $status_value);
        })
        ->when($account_type_select, function ($query, $account_type_select) {
            foreach ($account_type_select as $key => $value) {
                $account = explode(",", $value);
                $account_types = trim($account[0]);
                $account_currency = trim($account[1]);

                return $query->having('account_type_sub', '=', $account_types)
                             ->having('currency_id', '=', $account_currency);
            }
        })
        ->orderBy('id', 'desc')
        ->get();

Как это реализовать?


Ответы (0 шт):