Trying to get property of non-object laravel plz - php

#php
$package_banner_data=DB::table('accm_sys_media')->where('media_type','=',5)->where(AccmId,'=',$result['accm_sys_detail']->AccmId)->get();
#endphp
error:
ErrorException (E_ERROR)
Trying to get property 'AccmId' of non-object
application is developed in laravel
please help

The error surely comes from the line ...$result['accm_sys_detail']->AccmId ... You are trying to get the AccmId property of $result['accm_sys_detail'] object.
Verify that the $result['accm_sys_detail'] is an object. You can use
var_dump($result['accm_sys_detail'])
to see if the variable stores an object type.
Or use gettype() function to find out if the variable is carrying and object.

Related

errorexception trying to get property 'name' of non-object

As You can see after dd()
I am getting an object of "Note Category".
$note->category->name
it returns an error called trying to get property of non-object
dd() will stop execution, so you'd only get the dump for the first item in your loop. Try dump(...) instead, which will dump the same data as dd() but continue execution. There most likely is a note in your loop that doesn't have a category.

ErrorException (E_ERROR) Trying to get property 'id' of non-object In laravel 5.8

How To Fix??
I use laravel 5.8 and linux os
public function getAverageAttribute()
{
return (int)$this->reviews()->where('user_id', auth()->user()->id)->avg('rating');
}
Trying to get property 'id' of non-object
The user isn't authenticated. You will have to first check either the user is signed in or not. As auth()->user() is returning null, so the error is thrown when you are trying to access the id property on it.
Check first either the user is authenticated or not:
public function getAverageAttribute()
{
return auth()->user() ? (int)$this->reviews()->where('user_id', auth()->user()->id)->avg('rating') : false;
}
Debug your code, and you’ll see, what’s wrong.
E_ERROR notice, in your case is “trying to get property of non-object”, that the following piece of code returns non-object entity.
auth()->user()->id
var_dump this piece of code, and check what type actually returns.

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?
Edit:
When I do dd($post) with these 2 methods, I see exact result.
Also the error is getting logged in laravel.log file and I can see the page without any problem. This happen only when I use first() method.
Edit 2:
Here is my exact code:
$viewData['resource'] = Resource::select('id', 'resource_category_id', 'name', 'descp', 'seo_title', 'seo_keywords', 'seo_descp', 'dl_type', 'download', 'thumb', 'image', 'dl_count', 'updated_at')->with(['category' => function($q){
$q->select('id', 'name', 'slug', 'parent_id');
}])->where('slug', $slug)->first();
$id = $viewData['resource']->id; // this line throw error, line 126
local.ERROR: Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at E:\\websites\\couponclone\\app\\Http\\Controllers\\ResourceController.php:126)
Finally, I found the answer. This is the strange solution though. When you use Laravel firstOrFail() method. The error does not get logged in laravel.log file.
So snswer is use firstOrFail() instead of first(). This will not log Trying to get property of non-object error in your laravel log file.
I am not sure why this is so, may be due to PHP 7 or there is a bug in laravel? But it worked.

Notice: Trying to get property of non-object in /home/../feednew.php on line 161

$item[$value['field_name']] = stripwhitespace(nv_convert(strip_tags($element->find($el[0],$el[1])->innertext)));
Throws back this error:
Notice: Trying to get property of non-object in /home/../feednew.php on line 161** >> >innertext
This part of your code: $element->find($el[0], $el[1]) does not return any object. You have to find out why. Maybe, your search parameters $el[0] and $el[1] are wrong, maybe the function itself is incomplete. We can't get more information from this line of code.

How to get meaningful Laravel errors?

With errors like this:
ErrorException (E_UNKNOWN) Trying to get property of non-object
(View: /to/path/assign_template.blade.php)
How do I figure out line number where error is being produced?
In Laravel 4, on the left side you can click back down through the stack trace. Usually in views, 3-4 calls down will be the actual error that occurred in your view file.
That error is caused by trying to access the property of something that isn't an object, usually I find this is when trying to access relations to a model. Something like $model->relation->attribute. $model->relation in that case is usually null.

Categories