在Laravel 9中,我如何将结果从许多关系中获得许多关系
我在桌子和事故上有n:m关系,我确定了这一点,但是我无法将结果归还为“ AmpinClims”页面 这是我的代码: 事故模型
class Accident extends Model
{
use HasFactory;
public $timestamps = false;
protected $table = 'accidents';
protected $primaryKey = 'ReportNumber';
protected $fillable = [
'ReportNumber',
'Location',
'Date',
];
public function car()
{
return $this->belongsToMany(Car::class,'accident_participateds','reportNo','vehicleId');
}
汽车模型
class Car extends Model
{
use HasFactory;
public $timestamps = false;
protected $table = 'cars';
protected $fillable = [
'id',
'customer_id',
'color',
'model_no',
'company',
];
public function accidents()
{
return $this->belongsToMany(Accident::class,'accident_participateds','vehicleId','reportNo')->withPivot(['damageAmount', 'IBAN']);
}
}
枢轴表
class accidentParticipated extends Model
{
use HasFactory;
public $timestamps = false;
protected $primaryKey = ['reportNo','vehicleId'];
protected $table = 'accident_participateds';
protected $fillable = [
'reportNo',
'vehicleId',
'damageAmount',
'IBAN',
];
}
控制器
class AdminController extends Controller
{
public function AdminClaims()
{
$car = Car::with('accidents')->get();
$accident = Accident::with('car')->get();
return view('admin.AdminClaims',compact('car','accident'));
}
}
和最后一个视图
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>Report Number</th>
<th>Vehicle Id</th>
<th>Location</th>
<th>Date</th>
<th>Damage Amount</th>
<th>IBAN</th>
</tr>
</thead>
<tbody>
@foreach((array) $car as $car)
@foreach($car->$accident as $Acciden)
{<tr>
<td>{{$Acciden->pivot->ReportNumber}}</td>
<td>{{$Acciden->pivot->vehicleId}}</td>
<td>{{$Acciden->pivot->Location}}</td>
<td>{{$Acciden->pivot->Date}}</td>
<td>{{$Acciden->pivot->damageAmount}}</td>
<td>{{$Acciden->pivot->IBAN}}</td>
</tr>
@endforeach
@endforeach
</tbody>
</table>
</div>
</div>
我不知道错误在哪里 错误感受
Attempt to read property "[{"ReportNumber":123,"Location":"rood","Date":"2022-05-28","car":[{"id":9999999999,"customer_id":1233455432,"color":"red","model_no":2020,"company":"j class","pivot":
I have N:M relationship on tables Car and Accident, I establish that but I could not return the result in view "Adminclaims" page
here is my code:
Accident Model
class Accident extends Model
{
use HasFactory;
public $timestamps = false;
protected $table = 'accidents';
protected $primaryKey = 'ReportNumber';
protected $fillable = [
'ReportNumber',
'Location',
'Date',
];
public function car()
{
return $this->belongsToMany(Car::class,'accident_participateds','reportNo','vehicleId');
}
Car Model
class Car extends Model
{
use HasFactory;
public $timestamps = false;
protected $table = 'cars';
protected $fillable = [
'id',
'customer_id',
'color',
'model_no',
'company',
];
public function accidents()
{
return $this->belongsToMany(Accident::class,'accident_participateds','vehicleId','reportNo')->withPivot(['damageAmount', 'IBAN']);
}
}
Pivot Table
class accidentParticipated extends Model
{
use HasFactory;
public $timestamps = false;
protected $primaryKey = ['reportNo','vehicleId'];
protected $table = 'accident_participateds';
protected $fillable = [
'reportNo',
'vehicleId',
'damageAmount',
'IBAN',
];
}
Controller
class AdminController extends Controller
{
public function AdminClaims()
{
$car = Car::with('accidents')->get();
$accident = Accident::with('car')->get();
return view('admin.AdminClaims',compact('car','accident'));
}
}
And the last view
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>Report Number</th>
<th>Vehicle Id</th>
<th>Location</th>
<th>Date</th>
<th>Damage Amount</th>
<th>IBAN</th>
</tr>
</thead>
<tbody>
@foreach((array) $car as $car)
@foreach($car->$accident as $Acciden)
{<tr>
<td>{{$Acciden->pivot->ReportNumber}}</td>
<td>{{$Acciden->pivot->vehicleId}}</td>
<td>{{$Acciden->pivot->Location}}</td>
<td>{{$Acciden->pivot->Date}}</td>
<td>{{$Acciden->pivot->damageAmount}}</td>
<td>{{$Acciden->pivot->IBAN}}</td>
</tr>
@endforeach
@endforeach
</tbody>
</table>
</div>
</div>
I don't know where is the error
ErrorException
Attempt to read property "[{"ReportNumber":123,"Location":"rood","Date":"2022-05-28","car":[{"id":9999999999,"customer_id":1233455432,"color":"red","model_no":2020,"company":"j class","pivot":
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论