title: Build a blog in Laravel with post categories and user preferences
body: <p>I'm building a blog for fun. In my blog, I have users, posts, and a fixed list of predefined categories.</p>
  
  <p>1) Posts belong to many categories.
  2) Users can specify category preferences, and therefore belong to many categories.</p>
  
  <p>I have this set up as a series of many-to-many relationships:</p>
  
  <p>In <code>User.php</code>: </p>
  
  <pre><code>/**
  * A user belongs to many categories
  */
  public function categoryPreferences()
  {
      return $this-&gt;belongsToMany('App\Category');
  }
  </code></pre>
  
  <p>In <code>Post.php</code>:</p>
  
  <pre><code>/**
  * A Post Can Have Many Likes
  *
  * @return \Illuminiate\Database\Eloquent\Relations\belongsToMany
  */
  
  public function categories()
  {
  return $this-&gt;belongsToMany('App\Category');
  }
  </code></pre>
  
  <p>And the inverse of each in <code>Category.php</code>:</p>
  
  <pre><code>/**
  * A category belongs to many users
  */
  public function users()
  {
  return $this-&gt;belongsToMany('App\User');
  }
  
  /**
  * A category belongs to many posts.
  *
  * @return \Illuminiate\Database\Eloquent\Relations\belongsToMany
  */
  
  public function posts()
  {
  return $this-&gt;belongsToMany('App\Post');
  }
  </code></pre>
  
  <p>I have corresponding <code>category_post</code> and <code>category_user</code> join tables to support each relationship. However, I may be thinking about this incorrectly.</p>
  
  <p>I would like to display a loop of posts that belong to categories that users have specified, but can't work through how to do that. I can easily collect a user's category preferences with:</p>
  
  <p><code>$categories = $user-&gt;categoryPreferences-&gt;pluck('id');</code></p>
  
  <p>And can do the same for posts:</p>
  
  <p><code>$categories = $post-&gt;categories-&gt;pluck('id')</code></p>
  
  <p>However, I'm unsure how to merge the two collections together. I've looked into polymorphism, but that doesn't seem quite right. Can someone point me toward the correct concept?</p>