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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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)

Models

Reports

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

Member

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

        protected $table = 'members';

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

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

    }

Controller

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

Blade

{{ $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?

Solution

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

$document->poster->name 

//to

$document->poster['name']

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

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

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

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

Solution 2

//Write
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 infinityknow.com 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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Setup Laravel 6 Login Authentication
  2.   Create Bootable USB Drive Steps - Bootable Pen Drive
  3.   laravel 6 auth
  4.   authentication login with username or email in laravel
  5.   SQL SELECT SUM on multiple INNER JOIN with another table
  6.   Jquery simple rating system with star
  7.   Laravel 7 User Login Authentication From Scratch
  8.   Laravel Many to many relationships with itself
  9.   Reuse Code in jquery Ajax Methods
  10.   Simple React Hello World Example