Solution for “Trying to get property of non-object” in Laravel

If you working with or loops in laravel (for, foreach, etc.) display access the data to getting the try to get property of non-object laravel or any types of the relationships like as a (one to many, many to many, etc.), this may mean that one of the Database queries is returning a null data variable or a null laravel 5/6/7 relationship member.

Is your query returning array or object?

first of all you can check your results means output using dd() or dump() in laravel. and then

  • you might find that it’s an array => an array access ([])
  • find that it’s an object then => an access (->).

Problem : Trying to get property of non-object

we are trying to echo out the name of the member in my document and we are getting the ErrorException: Trying to get property of non-object. My source codes below: Exception Trying to get property of non-object (View: in blade file)

Also Read This πŸ‘‰   Laravel Bootstrap Toggle switch Update DB field using Ajax



class Reports extends Model
      public function postedBy()
         return $this->belongsTo('App\Member');
      protected $table = 'reports';
      protected $fillable = ['reportsContent', 'reportsTitle', 'postedBy'];


class Member extends Model implements AuthenticatableContract,
        use Authenticatable, Authorizable, CanResetPassword;

        protected $table = 'members';

        protected $fillable = ['name', 'email', 'password'];

        protected $hidden = ['password', 'remember_token'];



public function showArticle($slug)
        $document = Reports::where('slug', $slug)->firstOrFail();
        return view('document', compact('document'));


{{ $document->postedBy->name }}

When We try to remove name in the blade file like {{ $document->postedBy }} it outputs the id, but when We try to add the ->name there it says Trying to get property of non-object but We have a field name in my table and a Member model. Are We missing something?

Also Read This πŸ‘‰   laravel 6 auth


Is your query returning array or object? If you dump or dd it out, you might find that this is an array as well as all you need is an array access ([]) instead of an object access (->).

Second Opinion




Next is to included a second parameter in my belongsTo, from

return $this->belongsTo('App\Member');

return $this->belongsTo('App\Member', 'member_id');

in which member_id is my foreign key in the reports Database table.

trying to get property of non-object laravel foreach

Solution 1

<td>{{ $memberDetail['id'] }}</td> 
//instead of 
<td>{{ $memberDetail->id }}</td>

Solution 2

return view('backend.route_f_members.index', ['memberDetails'=>$memberDetails]);
//instead of
return view('backend.route_f_members.index', compact('memberDetails'));

I hope you get an idea about try to get property of non-object laravel.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.