我如何在具有关系属性的多对多上编写 Controller 函数
How I car write Controller function on many to many with relation attributes
我正致力于在 table 事故、汽车上应用多对多关系,其中关系如下:
在事故模型中:
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 个模型(汽车、事故、accident_participateds)的数据我的问题是这些语句在控制器中是否正确
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
请告诉我我在哪里犯了错误?
据我了解,您想显示 Accident
(索赔)、关联的 Car
和来自 pivot [=22] 的数据的详细信息=].
为此你可以试试
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
我正致力于在 table 事故、汽车上应用多对多关系,其中关系如下: 在事故模型中:
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 个模型(汽车、事故、accident_participateds)的数据我的问题是这些语句在控制器中是否正确
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
请告诉我我在哪里犯了错误?
据我了解,您想显示 Accident
(索赔)、关联的 Car
和来自 pivot [=22] 的数据的详细信息=].
为此你可以试试
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