I
I
Ilya Parshakov2018-03-24 15:39:33
Laravel
Ilya Parshakov, 2018-03-24 15:39:33

Why can't I access the 'image' property in $user->book->image?

Hello. I don't understand why I can't access the image property:

{{ $user->book['image'] }} // дает результат
{{ $user->book->image }} // не работает и выдает исключение: 
//<i>"Trying to get property 'image' of non-object"</i>

The User model has an association with Book belongsTo :
public function book()
    {
        return $this->belongsTo(Book::class);
    }

The Book model has a relationship with User hasMany :
public function users()
    {
        return $this->hasMany(User::class);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Osipov, 2018-03-25
@Alexxosipov

Image is a column in the books table? Please dump $user->book

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question