title: Join tables and fetch from Many to many (Laravel)
body: <p>User Table</p>
  
  <pre><code>    |---------------------|------------------|
      |          id         |     email        |
      |---------------------|------------------|
      |     3               |   18             |
      |---------------------|------------------|
  </code></pre>
  
  <p>Post Table</p>
  
  <pre><code>    |---------------------|------------------|
      |     id              |     Title        |
      |---------------------|------------------|
      |     12              |   My Post        |
      |---------------------|------------------|
  </code></pre>
  
  <p>Category Table</p>
  
  <pre><code>    |---------------------|------------------|
      |     id              |     Title        |
      |---------------------|------------------|
      |     18              |   My Category    |
      |---------------------|------------------|
  </code></pre>
  
  <p>Category-Post</p>
  
  <pre><code>    |---------------------|------------------|
      |     post_id         |     category_id  |
      |---------------------|------------------|
      |     12              |   18             |
      |---------------------|------------------|
  </code></pre>
  
  <p>User-Category</p>
  
  <pre><code>    |---------------------|------------------|
      |     user_id         |     category_id  |
      |---------------------|------------------|
      |     3               |   18             |
      |---------------------|------------------|
  </code></pre>
  
  <p>In this table structure, I want to fetch all the post with any given user id, according to their favorite category, with a single query.</p>
  
  <p>I have defined the relations for all the models</p>
  
  <p>Category:</p>
  
  <pre><code>public function posts(){
  
       $this-&gt;belongsToMany(Post::class,'category-post','category_id','post_id')
  }
  </code></pre>
  
  <p>Post:</p>
  
  <pre><code>public function categories(){
  
       $this-&gt;belongsToMany(Category::class,'category-post','post_id','category_id')
  }
  </code></pre>
  
  <p>User:</p>
  
  <pre><code>public function favCategories(){
           $this-&gt;belongsToMany(Category::class,'user-category-','user_id','category_id')
      }
  </code></pre>