Quantcast
Channel: Laravel : Traying to get property 'posts' of non-object - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Laravel : Traying to get property 'posts' of non-object

$
0
0

I wanted to show posts from a user. But, I received an error message : Trying to get property 'posts' of non-object

User.php

public function posts()
{
    return $this->hasMany(Post::class, 'author_id', 'id');
}

Post.php

public function author()
{
    return $this->belongsTo(User::class, 'author_id', 'id');
}

web.php

Route::get('test', function () {
    $user = User::find(5);
    return $user->posts;
});

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images