在Laravel 9中,我如何将结果从许多关系中获得许多关系

发布于 2025-02-02 02:24:55 字数 3504 浏览 2 评论 0原文

我在桌子和事故上有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文