Trying to Get Property of Non-Object - Laravel 5

Trying to get property of non-object - Laravel 5

Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).

Laravel : Trying to get property of non-object

your query returning array. If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).

Trying to get property 'id' of non-object

First of all: your post only has 1 user as writer(author) so make it user() instead of users()


  1. Add relationship to user model public function posts() { relationship is hasMany

  2. Get your data with eagle load $posts = Post::with('user')->get();

In that way you can have your author info in blade like:

@foreach($posts as $post)
{{$post->title}} <br>
{{$post->user->name}}
@endforeach

Hope it helps.

Laravel 5.8: Trying to get property 'prd_name' of non-object

From your information it looks like $arg['details'] is not coming empty and when fetching relation product in loop, one of the value of $detail is coming as null.

Try debugging for each order, what is the value of $arg['details'].

How can I fix this error Trying to get property 'title' of non-object in laravel 8

Look like some users doesn't have role .So better check for null.When you dd($user->roles->title) it only check for first user record not for all users.

@foreach($users as $user)
<p> {{ $user->roles->title??null }} </p>
@endforeach

Laravel Trying to get property 'name' of non-object. Where problem?

This is an array not an object, and needs to change $response :

foreach ($response['results'] as $result) {
Games::insert([
'name' => $result['name'],
'ratings' => $result['ratings'][0]['title'],
'platforms' => $result['platforms'][0]['platform']['name']
]);
}

Trying to get property 'point' of non-object

I solved it :
public function user_team($id)
{
$week = Week::where('status','active')->first();

    $week_id = $week->id;
$team = Team::where('user_id',$id)->with('userteams')->first();

foreach ($team->userteams as $userteam) {
$userteam->surname = $userteam->Club->surname;
$userteam->price = $userteam->Club->price;

$points = $userteam->Club->userpoints;
if(isset($oiints)){
foreach ($points as $point) {

if($point->user_id == $id){
$userteam->point = $point->point;
}
}
}else{
$userteam->point = 0;
}

unset($userteam->Club->userpoints);
unset($userteam->Club);

}
return $team;
}

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>


Related Topics



Leave a reply



Submit