我想过滤这个数组
$bank_totals
并仅选择amount值。
$bank_totals = $bank_totals->bank_balances();
"id" => 1
"bank" => "KCB"
"amount" => 7622.0
]
1 => array:3 [
"id" => 2
"bank" => "I & M Bank"
"amount" => 25000.0
从用户输入 $data['id'] 获取 id;我想要当 $data['id'] = 2 示例时显示的值为 25000 $data = request()->all(); $bank_totals = $bank_totals->bank_balances(); (出现在这里)
这是我的bank_balances方法
class TransactionsRepository
{
public function bank_balances(){
$banks_data = Bank::all();
$banks_totals = [];
foreach ($banks_data as $bank){
$totals = (BankingTransactions::where('bank_id', $bank->id)->sum('amount')) -
((PettyCash::where('bank_id', $bank->id)->sum('amount')) + ((PayDoctor::where('bank_id', $bank->id)
->sum('total_paid'))));
array_push($banks_totals,
[
'id'=>$bank->id,
'bank'=>$bank->name,
'amount'=>$totals,
]);
}
return $banks_totals;
}
}
将您的
bank_balances
方法替换为:
public function bank_balances($id = null) {
$banks_data = Bank::all();
$banks_totals = [];
if (is_null($id)) {
foreach ($banks_data as $bank) {
$totals = (BankingTransactions::where('bank_id', $bank->id)->sum('amount')) -
((PettyCash::where('bank_id', $bank->id)->sum('amount')) + ((PayDoctor::where('bank_id', $bank->id)
->sum('total_paid'))));
array_push(
$banks_totals,
[
'id' => $bank->id,
'bank' => $bank->name,
'amount' => $totals,
]
);
}
return $banks_totals;
} else {
return (BankingTransactions::where('bank_id', $id)->sum('amount')) -
((PettyCash::where('bank_id', $id)->sum('amount')) + ((PayDoctor::where('bank_id', $id)
->sum('total_paid'))));
}
}
并像这样使用它:
$bank_totals = $bank_totals->bank_balances($data['id']);