A
A
Aizharik2018-09-15 22:29:50
Laravel
Aizharik, 2018-09-15 22:29:50

Laravel when accessing a property Trying to get property of non-object?

The Pix model has a link to the Picture model

public function picture()
    {
        return $this->hasMany(Picture::class, 'pix_id');
    }

And Picture has a connection with Pix
public function pix()
    {
        return $this->belongsTo(Pix::class);
    }

If you get the first Picture model from Pix through the ->first() method, the Picture model is returned and the property is accessed, then an error occurs Trying to get property of non-object, if you do the same in dd(), then a call occurs, and outside dd() issues error Trying to get property of non-object
5b9d5d6aee3fb414080304.gif
Why is this happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-09-15
@aizhar777

Notice dd on dump and you will see on which iteration of the loop the error occurs and, therefore, which model has no pictures.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question