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 (->).
Read Also:  Word Count hadoop Program(in Java & Python)

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?

Read Also:  laravel 8 login and registration example | Laravel 8 Auth

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.

Read Also:  How to Implement Sweet Alert in Laravel Application

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. Read Also:  Update query in codeigniter Example
  2. Read Also:  Dynamically Add-remove Form Fields in jquery with PHP and mysql
  3. Read Also:  Handle CORS Requests in Laravel 6 Application
  4. Read Also:  Laravel 7 User Login Authentication From Scratch
  5. Read Also:  Vuejs Clone Array Example with Demo
  6. Read Also:  Setup Laravel 6 Login Authentication
  7. Read Also:  authentication login with username or email in laravel
  8. Read Also:  PHP Laravel Dynamically Add Active Navigation Class Example
  9. Read Also:  laravel 6 auth
  10. Read Also:  Redirect page after delay using Laravel Example
CLOSEX