获取收集Laravel

发布于 2025-02-04 02:52:10 字数 1836 浏览 4 评论 0原文

我是Laravel框架的新学习者 顾客, 事故, 执照 关系如下:

public  function License()
        {
            return $this->hasOne(license::class,'driver_id');
        }

    public function cars()
        {
            return $this->hasMany(car::class);
        }

我喜欢这样:

public function AdminCustomers()
    {
        $customers = Customer::with('cars')->with('License')->get();
        return view('admin.AdminCustomers',compact('customers'));
    }

现在我想在View.blade页面上显示所有3个模型的数据

<tbody>
                    @foreach($customers as $customer)

                                    <tr>
                                        <td>{{$customer->id}}</td>
                                        <td>{{$customer->name}}</td>
                                        <td>{{$customer->adderss}}</td>
                                        <td>{{$customer->mobileNo}}</td>
                                        <th>{{$customer->License->id}}</th>
                                        <th>{{$customer->License->Exp}}</th>
                                        <td>{{$customer->cars->id}}</td>
                                        <td>{{$customer->cars->color}}</td>
                                        <td>{{$customer->cars->model_no}}</td>
                                        <td>{{$customer->cars->company}}</td>
                                    </tr>
                               
                    @endforeach
                    </tbody>

在控制器中, 错误例外

Property [id] does not exist on this collection instance.

I am a new learner of the laravel framework working on 3 models
customer,
accidents,
License
where the relations are as follows:

public  function License()
        {
            return $this->hasOne(license::class,'driver_id');
        }

    public function cars()
        {
            return $this->hasMany(car::class);
        }

In the controller I do like this:

public function AdminCustomers()
    {
        $customers = Customer::with('cars')->with('License')->get();
        return view('admin.AdminCustomers',compact('customers'));
    }

now I want to display all 3 models' data on view.blade page

<tbody>
                    @foreach($customers as $customer)

                                    <tr>
                                        <td>{{$customer->id}}</td>
                                        <td>{{$customer->name}}</td>
                                        <td>{{$customer->adderss}}</td>
                                        <td>{{$customer->mobileNo}}</td>
                                        <th>{{$customer->License->id}}</th>
                                        <th>{{$customer->License->Exp}}</th>
                                        <td>{{$customer->cars->id}}</td>
                                        <td>{{$customer->cars->color}}</td>
                                        <td>{{$customer->cars->model_no}}</td>
                                        <td>{{$customer->cars->company}}</td>
                                    </tr>
                               
                    @endforeach
                    </tbody>

But it doesn't work and I didn't know where is the problem
the error Exception

Property [id] does not exist on this collection instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

止于盛夏 2025-02-11 02:52:10

$ customer-&gt; cars将返回集合,因为该关系是hasmany,因此您需要在视图中循环浏览集合

@foreach($customers as $customer)

    <tr>
        <td>{{$customer->id}}</td>
        <td>{{$customer->name}}</td>
        <td>{{$customer->adderss}}</td>
        <td>{{$customer->mobileNo}}</td>
        <th>{{$customer->License->id}}</th>
        <th>{{$customer->License->Exp}}</th>
            @foreach($customer->cars as $car)
                <td>{{$car->id}}</td>
                <td>{{$car->color}}</td>
                <td>{{$car->model_no}}</td>
                <td>{{$car->company}}</td>
            @endforeach
    </tr>
                               
@endforeach

$customer->cars will return a collection as the relationship is hasMany so you need to loop over the collection in the view

@foreach($customers as $customer)

    <tr>
        <td>{{$customer->id}}</td>
        <td>{{$customer->name}}</td>
        <td>{{$customer->adderss}}</td>
        <td>{{$customer->mobileNo}}</td>
        <th>{{$customer->License->id}}</th>
        <th>{{$customer->License->Exp}}</th>
            @foreach($customer->cars as $car)
                <td>{{$car->id}}</td>
                <td>{{$car->color}}</td>
                <td>{{$car->model_no}}</td>
                <td>{{$car->company}}</td>
            @endforeach
    </tr>
                               
@endforeach
寄风 2025-02-11 02:52:10

在DB上的Corrisponding表中检查名称,如果它与“ ID”不同,则将其指定为模型

protected $primaryKey = 'xxx';

check the name in the corrisponding table on db, and if its different from "id" specify it into the model, as

protected $primaryKey = 'xxx';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文