jeudi 27 juillet 2017

Get related posts based on common tag?

I have a posts table that has many-to-many relationship with tags table, connected using pivot table called tagspivot. I show a post using the following method :

public function showpost($titleslug) {
    $post = Post::where('titleslug','=',$titleslug)->first();
    return view('posts/show', compact('post', $post));
}

then I load the post tags in the view like :

@foreach($post->tags as $ptags)
  <li><a href="" class="button smallGrey">#</a></li>
@endforeach

My question is, how to get a list of posts that has the same tags with the current showing post ? it doesn't have to be exact same tags, like the other post has one or two common tags. If possible the list is sorted by the post that has most common tags with the current showing post.

That's all, sorry for my bad English



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire