我需要一个get字段值的总和。但我不能也不知道问题出在哪里。
controller
代码:
public function index()
{
$user1 = Auth::user();
$user2 = $user1->client_id;
$withdraws = Withdraw::where('client_id', $user2)->get();
$deposits = Deposit::where('client_id', $user2)->get();
return view('home')->with(compact('deposits', 'withdraws', ));
}
view
代码:
@foreach($deposits as $deposit)
<td id="count-data-td">{{ $deposit->amount }}</td>
@endforeach
return view('home')->with('transfers', array('deposits'=>$deposits, 'withdraws'=>$withdraws));
并查看代码
@foreach($transfers['deposits']) as $deposit)
{{ $deposit->amount }}
@endforeach
但如果你需要这笔钱,你总是可以这样做:
{{ $transfers['deposits']->sum('amount') }}
在您的情况下:
{{ $deposits->sum('amount') }}
控制器
public function index()
{
$user1 = Auth::user();
$user2 = $user1->client_id;
$withdraws = Withdraw::where('client_id',$user2)->get();
$deposits = Deposit::where('client_id', $user2)->get();
return view('home')->with(['deposits' => $deposits, 'withdraws',$withdraws]);
}
查看
{{ $deposits->sum('amount') }}
{{ $withdraws->sum('amount') }}
注意:您不需要运行foreach循环来获得总和