What triggers the Trying to get property of non-object error
When you do find($id)
and the record is not present, it returns null
. So if you try to access any property ->column
, it will throw error.
You can either do findOrFail($id)
or have condition if (is_null($data))
or on frontend have $data->column ?? 'defaultvalue
'
Now, the second situation where your model is present but you still get error, do :
$data = Table::find($id);
dd(array_keys($data->toArray()));
You should see the column
you are trying to access if it is present.
Lastly, if it is not present in above step but is there in the table, then check for protected $hidden = []
setting of your model which essentially hides certain columns.
php notice (trying to get property of non-object) error
Your function get_userdata()
will return False on failure, WP_User object on success.
And error Trying to get property of non-object
means that this function has returned false
.
Simply check if $userInfo
is not empty
$userInfo = get_userdata($userId);
if (!empty($userInfo)) {
$usersNames[] = $userInfo->display_name;
}
Severity: Notice Trying to get property of non-object and did not get any data from database table codeigniter
I did not get product_id.I forgot to name the 'product_id' in my view page.When i placed the name='product_id' in view page, this actually solved the problem.
Fix error trying to get property of non-object Laravel
You're getting multiple records if you use get()
method.
using first()
you'll get only one collection.
Remove get()
, and use only first()
method
$name = '';
$sub = DB::table('hours')->join('projects', function($join) use ($projectID,$id){
$join->on('hours.project_id', '=', 'projects.id')
->where('hours.project_id', '=', $projectID)
->where('hours.id', '=', $id);
})->select('nameP')->first();
if(!empty($sub)){
$name=$sub->nameP;
}
ERROR: Trying to get property id error in Laravel (non object)
I got it. the $categories
variable the you pass to the view is an array, with keys are category ids and values are category names. In your view, inside the loop, that $category
variable is a string, but you try to access that as an object ($category->id
) and get the error.
SOLUTION 1:
You can update your code like this:
<select class="form-control" name="category_id" class="form-control @error('category_id') is-invalid @enderror" required>
@foreach($categories as $categoryId => $categoryName)
<option value="{{ $categoryId }}" @isset($post) {{ $post->category->id == $categoryId ? 'selected' : '' }} @endisset>{{$categoryName}}</option>
@endforeach
<select class="form-control select2-multi" id="tags" name="tags[]" multiple>
@foreach($tags as $tagId => $tagName)
<option value="{{ $tagId }}" {{ old('tags[]', $post->tags)->contains($tagId) ? 'selected' : '' }}>{{ $tagName }}</option>
@endforeach
</select>
SOLUTION 2:
I see that in the controller action, you transform you categories and tags to arrays, which is not necessary. Just get those from the database and pass to the view.
public function edit($id)
{
// find the post in the database and save as a var
$post = Post::find($id);
$categories = Category::with('posts')->get();
$tags = Tag::with('posts')->get();
return view('backend.pages.posts.edit', [
'post' => $post,
'categories' => $categories,
'tags' => $tags,
]);
// Or you can even write: return view('backend.pages.posts.edit', compact('post', 'categories', 'tags'));
}
Then in your view:
<select class="form-control" name="category_id" class="form-control @error('category_id') is-invalid @enderror" required>
@foreach($categories as $category)
<option value="{{ $category->id }}" @isset($post) {{ $post->category->id == $category->id ? 'selected' : '' }} @endisset>{{$category->name}}</option>
@endforeach
<select class="form-control select2-multi" id="tags" name="tags[]" multiple>
@foreach($tags as $tag)
<option value="{{ $tag->id }}" {{ old('tags[]', $post->tags)->contains($tag->id) ? 'selected' : '' }}>{{ $tag->name }}</option>
@endforeach
</select>
Trying to get property 'id' of non-object only in my website
First of all, clear the caches with $promotion->id
as using the below command.
php artisan config:cache
php artisan view:clear
php artisan route:clear
If you still get the error then use $promotion->userId
as I have seen in your error the object contains userId
, not an id
.
Getting error- Trying to get property of non-object
$tenants
is an object, with the property tenants
. So, let's iterate it:
foreach($tenants->tenants as $row) {
?>
<tr>
<td data-field='name'><?php echo $row->name; ?></td>
...
Trying to get property of non-object - Laravel 5.6
The problem is that $surveys
is coming back null so you must check it before accessing some property:
public function home(Request $request)
{
$respondent = null;
$answers = null;
$yet_to_respond = null;
$no_response = null;
$surveys = Survey::where('user_id', Auth::user()->id)->orderBy('created_at','DESC')->first();
if($surveys){
$respondent = Invite::where('user_id', Auth::user()->id)->where('survey_id', $surveys->id)->count();
$answers = Invite::where('user_id', Auth::user()->id)->where('link_answered', 1)->where('survey_id', $surveys->id)->count();
$yet_to_respond = Invite::where('user_id', Auth::user()->id)->where('link_clicked', 1)->where('survey_id', $surveys->id)->count();
$no_response = Invite::where('user_id', Auth::user()->id)->where('link_clicked', 0)->where('survey_id', $surveys->id)->count();
}
return view('home', compact('surveys','respondent','yet_to_respond','no_response','answers'));
}
Another way is to use optional() method and instead using $surveys->id
you would be using optional($surveys)->id
The optional function accepts any argument and allows you to access properties or call methods on that object. If the given object is null, properties and methods will return null instead of causing an error:
Related Topics
Causes of MySQL Error 2014 Cannot Execute Queries While Other Unbuffered Queries Are Active
Posting Form Fields With Same Name Attribute
How to Install MySQLi on Macos
Multiple Files Upload in Codeigniter
Send Value of Submit Button When Form Gets Posted
Finding the Subsets of an Array in PHP
MySQLi_Query() Expects Parameter 1 to Be MySQLi, Object Given
How to Iterate Over the Results in a MySQLi Result Set
Fastest Way to Serve a File Using PHP
How Are Echo and Print Different in PHP
How to Show Multiple Recaptchas on a Single Page
PHP: Storing 'Objects' Inside the $_Session
PHP Foreach Pass by Reference: Last Element Duplicating (Bug)
How to Remove Array Element and Then Re-index Array
How to Detect Duplicate Values in PHP Array
Converting HTML to Plain Text in PHP For E-Mail