samedi 20 octobre 2018

Laravel Error: Trying to get property of non-object when accessing via first() method

I am using laravel 5.6 with PHP 7.1.

Laravel throws ErrorException Trying to get property of non-object when I try to access property of the post which is retrieved using eloquent first() method.

$post = Blog::select('id', 'title', 'slug')->where('slug', $slug)->first(); //slug is unique column in database
$post->title; //this line cause the error

However, If I use find() method, it works without any error.

$post = Blog::select('id', 'title', 'slug')->find($primarykey);
$post->title; //No error

Since, I can not use the second method, what is the best possible way to access these properties when modal is retrieved by first() method?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire