如何解决如何在父母索引页面的儿童表中显示儿童?
我有一个具有“ bank_name”,“ account_name”和“ balance”字段的银行模型。另一个模型“交易”是用户首先选择银行并输入“ opening_balance”和“ transaction_amount”,而“ opening balance + -transaction_amount”(-,+取决于借方/贷方)的“ closing_balance”在银行模型中变为“余额”。我想通过从事务处理表中获取close_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');
}
BankController:
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>
解决方法
您可以在银行模型中定义另一种关系,该关系为您提供一个模型对象,而不是像这样的数组:
public function transaction()
{
return $this->hasOne('App\Transaction');
}
然后,您可以在 bank.index 视图中使用此关系:
<td>{{$bank->transaction->closing_balance}}</td>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。