Hello @ajax30
The mistake is in:
$tag = Tag::firstWhere('id', $tag_id);
$articles = Article::where('id', $tag->id)->orderBy('id', 'desc')->paginate($this->per_page);
You load the article with the same id as the tag and not the articles for the tag :)
What I should do is:
$tag = Tag::firstWhere('id', $tag_id);
$articles = Article::query()
->whereHas('tags', function (Builder $query) use ($tag): void {
$query->where('id', $tag->id);
})
->orderBy('id', 'desc')
->paginate($this->per_page);
ps, I see on your tag model public function article()
but it can be multiple articles, for that I should call that function articles :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community