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
Contents
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.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.