我正在對表 Accident, Car 應用多對多關系,其中關系如下: 在 Accident 模型中:
public function car()
{
return $this->belongsToMany(Car::class,'accident_participateds',
'reportNo','vehicleId');
}
車載模型
public function accidents()
{
return $this->belongsToMany(Accident::class,'accident_participateds',
'vehicleId','reportNo')->withPivot(['damageAmount', 'IBAN']);
}
我想顯示所有 3 個模型(汽車、事故、事故參與)的資料我的問題是這些陳述句在控制器中是否正確
public function AdminClaims()
{
$claim = Accident::with('car')->get();
$details = Car::with('accidentsr')->get();
return view('admin.AdminClaims',compact('claim','details'));
}
因為當我嘗試這樣顯示時:
<tbody>
@foreach($claim as $claims)
<tr>
<td>{{$claims->ReportNumber}}</td>
@foreach($details as $AP)
<td>{{$AP->vehicleId}}</td>
<td>{{$AP->Location}}</td>
<td>{{$AP->Date}}</td>
<td>{{$AP->damageAmount}}</td>
<td>{{$AP->IBAN}}</td>
@endforeach
</tr>
@endforeach
它只列印 ReportNumber 請告訴我哪里出錯了?

uj5u.com熱心網友回復:
據我了解,您希望顯示資料透視表中的Accident(宣告)、關聯Car和資料的詳細資訊。
為此,您可以嘗試
public function AdminClaims()
{
$claims = Car::with('accidents')->get();
return view('admin.AdminClaims',compact('claims'));
}
并且在視圖中
@foreach($claims as $claim)
<tr>
<td>{{$claim->ReportNumber}}</td>
<td>{{$claim->vehicleId}}</td>
<td>{{$claim->Location}}</td>
<td>{{$claim->Date}}</td>
<td>{{$claim->pivot->damageAmount}}</td>
<td>{{$claim->pivot->IBAN}}</td>
</tr>
@endforeach
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485549.html
