title: Laravel 5: how can I retrieve and display all posts that belong to certain category
body: <p>I have 3 tables</p>
  
  <pre><code>user:       id, username
  subreddits: id, name, created_at
  posts:      id, title, link, user_id, subreddit_id
  </code></pre>
  
  <p>The problem is, I am fetching the id of the subreddit / category manually while I need to be fetching it dynamically. How can I achieve that?</p>
  
  <p>This is the <code>show()</code> method in SubredditController.php</p>
  
  <pre><code>public function show(Subreddit $subreddit)
      {
  
          $posts = Subreddit::findOrFail(3)-&gt;posts()-&gt;get();
  
          return view('subreddit/show')-&gt;with('subreddit', $subreddit)
                                      -&gt;with('posts', $posts);
      }
  </code></pre>
  
  <p>And this is Subreddit Model</p>
  
  <pre><code>class Subreddit extends Model
  {
      protected $fillable = [
          'name',
          'description'
      ];
  
      public function user() {
          return $this-&gt;belongsTo('App\User');
      }
  
      public function posts() {
          return $this-&gt;hasMany('App\Post');
      }
  }
  </code></pre>
  
  <p>routes.php</p>
  
  <pre><code>Route::resource('subreddit', 'SubredditController');
  Route::resource('posts', 'PostsController');
  </code></pre>
  
  <p>RouteServiceProvider.php</p>
  
  <pre><code>$router-&gt;model('articles', 'App\Article');
  $router-&gt;model('subreddit', 'App\Subreddit');
  $router-&gt;model('posts', 'App\Post');
  </code></pre>