Search code examples
phplaraveleloquenthas-manybelongs-to

Property [customInput] does not exist on this collection instance


I have 2 models linked with each other: Link and Entries. Inside my entries table I made a foreignId called link_id.

Entry model:

class Entry extends Model
{

use HasFactory;

protected $guarded = [];

protected $table = 'entries';

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

Link model:

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

When a visitor visits the link an entry is made. The link contains some values, like a title etc.

Now I also made an admin panel where I can upload data to a 'customInput' field in the database as a string. I just don't know how to get that data because when I try to use $link->entries->customInput, Laravel returns this error: Property [customInput] does not exist on this collection instance.

How can I fix this?


Solution

  • $link->entries is a collection, so you need a loop to get the value from this collection :

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