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;
});