【发布时间】:2020-09-06 11:53:53
【问题描述】:
我有一个具有“bank_name”、“account_name”和“balance”字段的银行模型。另一个模型交易是用户首先选择银行并输入opening_balance和transaction_amount,而closeing_balance是“期初余额+- transaction_amount”(-,+取决于借方/贷方)成为银行模型中的“余额”。我想通过从 transactions 表中获取 closing_balance 来显示 bank.index 页面中特定银行的余额。我被困在这里。到目前为止,我有:
银行模型:
protected $fillable=['bank_name','account_name'];
public function transactions()
{
return $this->hasMany('App\Transaction');
}
交易模型:
protected $fillable = ['bank_id','opening_balance','transaction_amount','isdebit','closing_balance'];
public function bank()
{
return $this->belongsTo('App\Bank');
}
银行控制器:
public function index()
{ $banks = Bank::all();
//$bal_1 = Bank::find(1)->transactions()->latest()->first();
// I can show the balance in bank which has id 1 by this manually.
return view('bank.index', compact('banks','bal_1'));
}
事务控制器:
public function index()
{ $transactions = Transaction::all();
return view('transaction.index',compact('transactions'));
}
Bank.index 页面
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Bank Name</th>
<th scope="col">Account Name</th>
<th scope="col">Balance</th>
</tr>
</thead>
<tbody>
@foreach($banks as $i=>$bank)
<tr>
<th scope="row">{{++$i}}</th>
<td>{{$bank->bank_name}}</td>
<td>{{$bank->account_name}}</td>
<td>{{$bal_1->closing_balance}}</td>
</tr>
@endforeach
</tbody>
</table>
【问题讨论】:
标签: php laravel eloquent model controller