访问具有一对多关系的子属性
accessing child attribute with one to many relationship
我有模型 Member
、Loan
和 Interestamount
。 Loan
与 Interestamount
有 hasMany
关系。我似乎无法从 Interestamount
.
访问数据
我可以在 blade 中显示贷款 id
和 Interestamount
,但无法为给定的 Loan
排序 Interestamount
。
LoanController.php
public function loanInterest($criteria){
//$loanData = Loan::all();
$loanData =Loan::findOrFail($criteria);
return view($this->_pagePath.'loan.loaninterest',compact('loanData'));
}
web.php
Route::any('loaninterest/{criteria?}','LoanController@loanInterest')
->name('loaninterest');
Loan.php
use Illuminate\Database\Eloquent\Model;
class Loan extends Model
{
protected $fillable = [
'amount',
'interest',
'status',
'duration',
'member_id',
'loan_type_id',
'interest_type_id',
'loan_payment_type_id'
];
// protected $appends = 'interest_amount
public function getInterestAmountAttribute()
{
return ($this->amount)/100 * $this->interest;
}
public function interestamount()
{
return $this->hasMany(InterestAmount::class,'loan_id','id');
}
}
InterestAmount.php
use Illuminate\Database\Eloquent\Model;
class InterestAmount extends Model
{
protected $fillable = ['interestamount'];
public function loan()
{
return $this->belongsTo(Loan::class,'loan_id','id');
}
}
loaninterest.blade.php
<tr>
<td>{{$loanData->member->name}}</td>
@foreach($loanData->interestamount() as $int)
<td>{{$int->interestamount}} </td>
@endforeach
</tr>
loan.blade.php
<a href="{{route('loaninterest', $loan->id) }}">Interest detail</a>
将您的贷款利息更改为此
<td>{{$loanData->member->name}}</td>
@foreach($loanData->interestamount as $int)
<td>{{$int->interestamount}} </td>
@endforeach
</tr>
当我们使用 $loanData->interestamount()
时,它指的是一个查询构建器,但是当我们使用 $loanData->interestamount
时,它 returns 与 $loanData->interestamount()->get()
相同的相关集合
$loanData->interestamount()
returns 查询生成器实例而不是查询结果。
有几种方法可以从关系函数中获取结果。
其中之一是调用get()
函数
例子
$loanData->interestamount()->get();
另一种方法是调用关系函数not as a function but as a property
例子
$loanData->interestamount;
因此在您的 blade 文件中 @foreach()
@foreach($loanData->interestamount as $int)
<td>{{$int->interestamount}} </td>
@endforeach
我不知道原因,但我这样做了并且成功了。
<td>{{$loanData->member->name}}</td>
@foreach($loanData->interestamount()->get() as $int)
<td>{{$int->interestamount}} </td>
@endforeach
我有模型 Member
、Loan
和 Interestamount
。 Loan
与 Interestamount
有 hasMany
关系。我似乎无法从 Interestamount
.
我可以在 blade 中显示贷款 id
和 Interestamount
,但无法为给定的 Loan
排序 Interestamount
。
LoanController.php
public function loanInterest($criteria){
//$loanData = Loan::all();
$loanData =Loan::findOrFail($criteria);
return view($this->_pagePath.'loan.loaninterest',compact('loanData'));
}
web.php
Route::any('loaninterest/{criteria?}','LoanController@loanInterest')
->name('loaninterest');
Loan.php
use Illuminate\Database\Eloquent\Model;
class Loan extends Model
{
protected $fillable = [
'amount',
'interest',
'status',
'duration',
'member_id',
'loan_type_id',
'interest_type_id',
'loan_payment_type_id'
];
// protected $appends = 'interest_amount
public function getInterestAmountAttribute()
{
return ($this->amount)/100 * $this->interest;
}
public function interestamount()
{
return $this->hasMany(InterestAmount::class,'loan_id','id');
}
}
InterestAmount.php
use Illuminate\Database\Eloquent\Model;
class InterestAmount extends Model
{
protected $fillable = ['interestamount'];
public function loan()
{
return $this->belongsTo(Loan::class,'loan_id','id');
}
}
loaninterest.blade.php
<tr>
<td>{{$loanData->member->name}}</td>
@foreach($loanData->interestamount() as $int)
<td>{{$int->interestamount}} </td>
@endforeach
</tr>
loan.blade.php
<a href="{{route('loaninterest', $loan->id) }}">Interest detail</a>
将您的贷款利息更改为此
<td>{{$loanData->member->name}}</td>
@foreach($loanData->interestamount as $int)
<td>{{$int->interestamount}} </td>
@endforeach
</tr>
当我们使用 $loanData->interestamount()
时,它指的是一个查询构建器,但是当我们使用 $loanData->interestamount
时,它 returns 与 $loanData->interestamount()->get()
$loanData->interestamount()
returns 查询生成器实例而不是查询结果。
有几种方法可以从关系函数中获取结果。
其中之一是调用get()
函数
例子
$loanData->interestamount()->get();
另一种方法是调用关系函数not as a function but as a property
例子
$loanData->interestamount;
因此在您的 blade 文件中 @foreach()
@foreach($loanData->interestamount as $int)
<td>{{$int->interestamount}} </td>
@endforeach
我不知道原因,但我这样做了并且成功了。
<td>{{$loanData->member->name}}</td>
@foreach($loanData->interestamount()->get() as $int)
<td>{{$int->interestamount}} </td>
@endforeach