I want to display tags of the posts , but when I tried to display, tags only appear in the last post.
Here my codes;
BlogController;
public function getSingle($slug){
$posts = Post::where('slug', $slug)->first();
return view('front.pages.single')->with('post', $posts);
}
Single.blade.php
<div class="blog-main">
{!! $post->body !!}
<hr>
@if($post->tags()->count() == 0)
{{"Add Some Tags"}}
@elseif($post->tags()->count() > 0)
<p> <strong>Tags:</strong>
@foreach($post->tags as $tag)
<span class="btn btn-outline-primary btn-sm" ><a href="{{route('tags.show',$tag->id)}}" >{{$tag->name}} </a></span>
@endforeach </p>
@endif
</div>
Relationship with Tags and Posts
Post Model
public function tags(){
return $this->belongsToMany('App\Tag');
}
Tag Model
public function posts(){
return $this->belongsToMany('App\Post');
}
Where do I mistake ?