public function posts()
{
return $this->belongsToMany('App\Models\Post', 'post_hashtag_pivot', 'hashtag_id', 'post_id');
}
and then in the Post model
public function hashtags()
{
return $this->belongsToMany('App\Models\Hashtag', 'post_hashtag_pivot', 'post_id', 'hashtag_id');
}
you don't reference the id on the pivot table.
and then
$tag = 'nice';
$nicePosts = Post::whereHas('hashtags', function($query) use($tag){
$query->where('name',$tag);
})->get();