属性 [customInput] 在此 collection 实例上不存在

Property [customInput] does not exist on this collection instance

我有 2 个模型 link 相互编辑:Link 和条目。 在我的条目 table 中,我创建了一个名为 link_id.

的 foreignId

参赛车型:

class Entry extends Model
{

use HasFactory;

protected $guarded = [];

protected $table = 'entries';

public function link() {
    return $this->belongsTo(Link::class);
}

Link 型号:

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);
}

当访问者访问 link 时,将创建一个条目。 link 包含一些值,如标题等。

现在我还制作了一个管理面板,我可以在其中将数据作为字符串上传到数据库中的 'customInput' 字段。 我只是不知道如何获取该数据,因为当我尝试使用 $link->entries->customInput 时,Laravel returns 此错误:属性 [customInput ] 在这个 collection 实例上不存在。

我该如何解决这个问题?

$link->entries 是一个集合,所以你需要一个循环来从这个集合中获取值:

@foreach($link->entries as $data)
   {{ $data->customInput }}
@endforeach