title: laravel get related posts by tags
body: <p>I want to get the related posts of a current post by tags but honestly I can't get it.</p>
  
  <p>I will show you my tables structure.</p>
  
  <p>Posts Table:</p>
  
  <pre><code>public function up()
  {
      Schema::create('posts', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('user_id')-&gt;unsigned();
          $table-&gt;string('title');
          $table-&gt;string('slug')-&gt;unique();
          $table-&gt;text('body');
          $table-&gt;text('excerpt')-&gt;nullable();
          $table-&gt;string('stickers')-&gt;nullable();
          $table-&gt;integer('category_id')-&gt;nullable()-&gt;unsigned();
          $table-&gt;text('meta_description')-&gt;nullable();
          $table-&gt;text('meta_keywords')-&gt;nullable();
          $table-&gt;string('postimg')-&gt;nullable();
          $table-&gt;string('type')-&gt;nullable()-&gt;default('common');
          $table-&gt;boolean('published')-&gt;default(false);
          $table-&gt;softDeletes();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>Tags Table:</p>
  
  <pre><code>public function up()
  {
      Schema::create('tags', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name')-&gt;unique();
          $table-&gt;string('slug')-&gt;unique();
          $table-&gt;softDeletes();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>I have a pivot table to handle tags on posts and posts with those tags.</p>
  
  <p>post_tag table:</p>
  
  <pre><code>public function up()
  {
      Schema::create('post_tag', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('post_id')-&gt;unsigned();
          $table-&gt;foreign('post_id')-&gt;references('id')-&gt;on('posts')-&gt;onDelete('cascade');
  
          $table-&gt;integer('tag_id')-&gt;unsigned();
          $table-&gt;foreign('tag_id')-&gt;references('id')-&gt;on('tags')-&gt;onDelete('cascade');
      });
  }
  </code></pre>
  
  <p>Everything is working good, one tag have many posts and one post have many tags, is a many to many relationship.</p>
  
  <p>Post Model:</p>
  
  <pre><code>public function tags()
  {
      return $this-&gt;belongsToMany('App\Tag');
  }
  </code></pre>
  
  <p>Tag Model:</p>
  
  <pre><code>public function posts()
  {
    return $this-&gt;belongsToMany('App\Post');
  }
  </code></pre>
  
  <p>I can display all the tags on a post but i want to display the related posts by tags, when i say related posts i meaning to "the current post" that visitor is reading. Let's say that this current post have a microsoft, google, apple, cars tags, i want the related posts to those tags. I don't know if that's possible or is easyer to do ir by categories.</p>
  
  <p>News Controller logic:</p>
  
  <p>Here's where i have all the logic to the post view.</p>
  
  <pre><code>public function getSingle($slug, $id = null)
  {
      $post = Post::where('slug', '=', $slug)-&gt;first();
      $topcat = Category::orderBy('created_at', 'desc')-&gt;limit(5)-&gt;get();
      $comment = Comment::find($id);
  
      $tags = Tag::all();
      $tags2 = array();
      foreach ($tags as $tag) {
          $tags2[$tag-&gt;id] = $tag-&gt;name;
      }
  
      // Previous and Next Post
      $previous = Post::where('id', '&lt;', $post-&gt;id)-&gt;orderBy('id', 'desc')-&gt;first();
      $next = Post::where('id', '&gt;', $post-&gt;id)-&gt;orderBy('id', 'asc')-&gt;first();
  
      // Related Posts Here!
  
      $tags3 = array();
      foreach ($post-&gt;tags as $tag) {
          $tags3[$tag-&gt;id] = $tag-&gt;name;
      }
      $related = Post::whereHas('tags', function ($query) use ($tags3) {
          $query-&gt;where('name', $tags3);
      })-&gt;get();
      // dd($related);
  
      return view('news.single')
              -&gt;withPost($post)
              -&gt;withTopcat($topcat)
              -&gt;withTags($tags2)
              -&gt;withComment($comment)
              -&gt;withPrevious($previous)
              -&gt;withNext($next)
              -&gt;withRelated($related);
  }
  </code></pre>
  
  <p>I did the <code>$tags3</code> variable to test it that way but i didn't get what i wanted.</p>
  
  <p>Thanks in advance</p>