【问题标题】:Property [customInput] does not exist on this collection instance此集合实例上不存在属性 [customInput]
【发布时间】:2021-02-08 03:37:39
【问题描述】:

我有 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。

我该如何解决这个问题?

【问题讨论】:

    标签: php laravel eloquent has-many belongs-to


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2019-03-23
      • 2020-07-11
      • 2017-05-12
      • 2018-10-12
      • 2021-12-16
      • 2021-09-14
      相关资源
      最近更新 更多