title: Laravel query with hasmany relationship
body: <p>I'm new to laravel so forgive me for the possible noobish question. Also, I did look into all the other 'similar' questions but either they're don't reproduce the right solution or i'm honestly having a hard time wrapping my head around it.</p>
  
  <p>The scenario:</p>
  
  <p>I have a Post model and a Subject model. This is what they look like at the moment.</p>
  
  <p>in Post.php</p>
  
  <pre><code>public function subjects() {
      return $this-&gt;belongsToMany('Subject', 'posts_subjects');
  }
  </code></pre>
  
  <p>in Subject.php</p>
  
  <pre><code>public function posts() {
      return $this-&gt;belongsToMany('Post', 'posts_subjects');
  }
  </code></pre>
  
  <p>Now, what i need to achieve is: </p>
  
  <p>In case an query parameter is passed to the request(ie q=Food) i want to return only the posts that contain the subject food in their subjects relationship and none of the others. if nothing is passed, then i just show everything...</p>
  
  <p>This is what my PostsController's index action looks like atm.</p>
  
  <pre><code>public function index() {
  
      $q = Input::get('q');
  
      $posts = Post::all();
  
      return View::make('posts.index', ['posts' =&gt; $posts]);
  }
  </code></pre>
  
  <p>How would i go about doing that? would greatly appreciate some help.
  Thanks a lot</p>
  
  <p>PS. I'm able to get all the posts with this code.</p>