laravel - 3张表的雄辩关系
问题描述
我想在 user.show 上显示授予特定用户的贷款。
这是赠款贷款表
现在在 user.show 我想显示授予该用户的贷款
在我的贷款金额模型上,
public function grantLoans()
{
return $this->manyToMany('App\LoanAmount','id','amount_id');
}
在我的 GrantLoanAmount 模型上
public function user() //just update this. this line works
{
return $this->hasMany('App\User','id','user_id');
}
public function amounts() //just update this. this line works
{
return $this->hasMany('App\LoanAmount','id','amount_id');
}
在我的用户控制器上
目前我只有这个,
public function show($id)
{
$grantLoan = GrantLoanAmount::where('user_id',$id)->get();
?????
}
我怎么能金额?形成grant_loan_amounts 表?
谢谢你!
解决方案
假设您有一个User
模型,一个LoanAmount
模型和一个GrantLoanAmount
模型,并且可以向用户授予多个贷款金额,那么这应该可行
//User model
public function loan_amounts()
{
return $this->hasManyThrough(
'App\LoanAmount',
'App\GrantLoanAmount',
'user_id',
'id',
'id',
'amount_id'
);
}
推荐阅读
- ios - 当任何事情发生时,一切都会暂时为零
- jquery - 物化css datepicker yearRange不工作
- android - 如何使用 volley 以新的意图解析 json 文件
- hyperledger-composer - 如何在使用 Mocha 进行测试时捕获交易
- python - 使用 os.walk 从 dir 及其子目录中获取所有文件
- sqlite - Android:从 SQLite 数据库填充 GridtView
- hibernate - 调用 setter 时发生 IllegalArgumentException
- java - 如何在java中更改二维数组中值的位置?
- mysql - 来自不同表的求和结果,如果不存在则返回null
- ecmascript-6 - ES6 JS Promises - 如何避免条件嵌套