php - 尝试在刀片中获取非对象属性时出错
问题描述
所以我试图传递数据来查看:
$orders = $this
->query()
->where('user_id', '=', $id)
->with(['payment', 'payment.paymentStatus'])
->get();
return view('profile.profile-orders', compact('orders'));
我得到这个有效载荷:
{
"id": 4,
"order_id": 4,
"status": 1,
"price": 897,
"payment_date": "2021-10-11 18:11:50",
"payment_status": {
"id": 1,
"title": "Not paid",
"status_color": "#d1dade",
},
"order": {
"id": 4,
"customer_id": 4,
"created_at": "2021-10-11T18:11:33.000000Z",
"updated_at": "2021-10-11T18:11:33.000000Z",
"items": [
{
"id": 5,
"amount": 1,
"price": 697,
"product": {
"id": 1,
"price": "697.00",
"order": 1,
"images": [
{
"img": "image1"
},
{
"img": "image2"
}
],
}
}
]
}
}
在我看来,我试图得到payment_status->title
@foreach($orders as $order)
<tr>
<td>{{ $order->payment->payment_status->title }}</td>
</tr>
@endforeach
但我收到此错误Trying to get property 'title' of non-object
如何解决此错误?
解决方案
我认为您需要的只是数组访问([])
而不是对象访问(->)
。所以你的代码应该是:
@foreach($orders as $order)
<tr>
<td>{{ $order->payment->payment_status['title'] }}</td>
</tr>
@endforeach
推荐阅读
- javascript - 将来自多个 http.requests 的数组组合成一个数组
- php - 实时服务器上的窗口系统中的新选项卡 pdf 打开问题
- reactjs - 如何添加到 Gatsby 配置文件的链接
- html - 具有相对大小的固定标题
- javascript - 如何使用自定义虚拟键盘模拟真实键盘事件?
- python - 如何将通过 tkinter 中的循环创建的 Radiobuttons 值设置为默认值?
- javascript - 将项目推送到嵌套对象数组
- docker-compose - services.networks 不支持的配置选项:“ipam”
- r - 将 VBA 代码翻译成 R 代码的问题
- java - 等待所有 JDA RestActions