This is my Controller method wherein I want to fetch posts by user id. I also have relationship defined in User model
public function posts()
{
return $this->hasMany('Post','user_id','id');
}
UserController.php
use App\Post;
public function findPostsByUser($id){
$user = User::findOrFail($id);
$posts = $user->posts()->get();
return $posts;
}
But it is giving me 'Post' not found error. Can someone help on this.
>hasMany('Post','user_id','id')to>hasMany('App\Post','user_id','id')Post::where('user_id', $id)->get()