php - 无法从产品表中指定“标题” - php artisan tinker
问题描述
使用php artisan tinker
我可以拉出一个产品表,在这个例子中,我的数据库中的第一个用户
>>> App\User::first()->product;
=> Illuminate\Database\Eloquent\Collection {#2912
all: [
App\product {#2898
id: 2,
owner_id: 2,
title: "ListingTestTitle",
description: "ListingTestDescription",
price: "420.69",
created_at: "2019-11-08 13:21:28",
updated_at: "2019-11-08 13:21:28",
},
],
}
但是,当我尝试进一步进入这个集合并抓住标题时,我收到以下错误
>>> App\User::first()->product->title;
Exception with message 'Property [title] does not exist on this collection instance.'
无论我尝试提取哪个属性,我都会遇到同样的问题。
解决方案
您可以在模型中使用hasOne
关系User
App\User::first()->product->first()->title;
推荐阅读
- jms - IBM MQ - 如何使用多个连接名称连接到队列管理器(一个是故障转移)
- haskell - 在 Haskell 中更改映射中多个键的值
- python - 用双引号括起来时忽略逗号
- excel - 如何将单元格范围复制为电子邮件正文中的位图?
- voyager - Nuxt+Vuetify 和 Directus 哪个更好?
- amazon-web-services - 使用节点后端托管 Angular 前端
- laravel - 更改全日历语言
- powershell - 如何通过powershell列出sharepoint在线目录中的文件
- python - 如何使python函数将封闭变量绑定到值而不是名称
- android - Angular Router Resolve 在任务终止并重新打开 Android Google Chrome 时不起作用