php - 此集合实例上不存在属性 [customInput]
问题描述
我有 2 个相互链接的模型:链接和条目。在我的条目表中,我创建了一个名为 link_id 的 foreignId。
入门型号:
class Entry extends Model
{
use HasFactory;
protected $guarded = [];
protected $table = 'entries';
public function link() {
return $this->belongsTo(Link::class);
}
链接型号:
class Link extends Model
{
use HasFactory;
use Uuids;
protected $guarded = [];
public function user() {
return $this->belongsTo(User::class);
}
public function entries() {
return $this->hasMany(Entry::class);
}
当访问者访问该链接时,就会创建一个条目。该链接包含一些值,例如标题等。
现在我还制作了一个管理面板,我可以在其中将数据作为字符串上传到数据库中的“customInput”字段。我只是不知道如何获取该数据,因为当我尝试使用 $link->entries->customInput 时,Laravel 会返回此错误:Property [customInput] does not exist on this collection instance。
我怎样才能解决这个问题?
解决方案
$link->entries
是一个集合,所以你需要一个循环来从这个集合中获取值:
@foreach($link->entries as $data)
{{ $data->customInput }}
@endforeach
推荐阅读
- node.js - Webpack devServer 不会代理从前端到后端服务器的请求
- jenkins-pipeline - 从 Jenkins 管道中的资源目录中获取所有文件
- python - pandas index - 按数字子字符串对字符串索引进行排序
- makefile - 使依赖重复规则
- java - 转换列表
进入 JSONObject - python - 如何创建锚定到左下角的网格?
- ios - 如何更改 SwiftUI Textfiled 的字体
- typescript - 返回具有额外属性的 arg 的 TypeScript 函数 (TS2322)
- r - 合并/组合 4 个变量
- vue.js - Vue:实例上未定义属性或方法