title: Laravel: retrive and paginate all post belonging to one specific category
body: <p>Ok, so I saw similar questions asked but none of the answers helped, so forgive me if it seems like this is a duplicate.</p>
  
  <p>I am following a course on Udemy(It's been over a week, the instructor hasn't answered my question, although they are active on the site, hence why I'm here), the codes I'm displaying below is how the instructor has taught us to write it.</p>
  
  <p>THE ISSUE: 
  We are creating a blog and that blog has many categories and many posts but each post belongs to only ONE category which can hold multiple posts.
  Here's a look at the model for them. </p>
  
  <pre><code>public function user(){
      return $this-&gt;belongsTo('App\User');
  }
  
  public function posts(){
      return $this-&gt;hasMany('App\Post');
  }
  </code></pre>
  
  <p>POST MODEL </p>
  
  <pre><code>public function user(){
      return $this-&gt;belongsTo('App\User');
  }
  
  public function category(){
      return $this-&gt;belongsTo('App\Category');
  }
  </code></pre>
  
  <p>They each have their own controller also, we also have a frontend controller that displays all the categories and when you click read more it takes you to a single page that displays the most recent post within that category. 
  This single page also has a pagination that supposed to display the previous post that belongs to that category. 
  INSTEAD: it takes the user to the most recent post regardless of what category it belongs to and display all the previous post for all category.
  Below is the frontend controller </p>
  
  <pre><code>        public function index()
  {
      return view('categories')
             -&gt;with('categories',Category:orderBy('created_at', 'desc')-&gt;take(24)-&gt;get())
             -&gt;with('tag', Tag::all())
             -&gt;with('posttitle', Post::orderBy('created_at', 'desc')-&gt;first());
  }
  
  public function tag($id)
  {
      $tag = Tag::find($id);
  
      return view('tag')-&gt;with('tag', $tag)
                          -&gt;with('categories', $tag-&gt;tag)
                          -&gt;with('categories', Category::all());
  }
  
  public function singlePage($slug)
  {
      $post = Post::where('slug', $slug)-&gt;first();
  
      $next_id = Page::where('id', '&gt;', $page-&gt;id)-&gt;min('id');
      $prev_id = Post::where('id', '&lt;', $post-&gt;id)-&gt;max('id');
  
      return view('single')-&gt;with('post', $post)
                                   -&gt;with('categories', Category::all())
                                   -&gt;with('posttitle', Post::take(1)-&gt;get())
                                   -&gt;with('next', Post::find($next_id))
                                   -&gt;with('prev', Post::find($prev_id));                                     
  }
  </code></pre>
  
  <p>Here's a look at the view for the index function </p>
  
  <pre><code>&lt;div class="container"&gt;
  
  &lt;div class="container-fluid"&gt;
      &lt;div class="row medium-padding120 bg-border-color"&gt;
          &lt;div class="container"&gt;
              &lt;div class="col-lg-12"&gt;
              &lt;div class="offers"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-lg-8 col-md-12 col-sm-12 col-xs-12"&gt;
                          &lt;div class="heading"&gt;
                              &lt;h4 class="h2 heading-title"&gt;Discover New Articles&lt;/h4&gt;
                              &lt;div class="heading-line"&gt;
                                  &lt;span class="short-line"&gt;&lt;/span&gt;
                                  &lt;span class="long-line"&gt;&lt;/span&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="row"&gt;
                      &lt;div class="case-item-wrap"&gt;
                          @foreach($categories as $category)
                          &lt;div class="col-lg-4 col-md-4 col-sm-6 col-xs-12"&gt;
                              &lt;div class="case-item"&gt;
                                  &lt;div class="case-item__thumb"&gt;
                                      &lt;img src="{{ $category-&gt;featured_img }}" alt="{{ $category-&gt;title }}"style="width:300px; height:280px;"&gt;
                                  &lt;/div&gt;
                                  &lt;h6 class="case-item__title text-center"&gt;&lt;a href="#"&gt;{!! $category-&gt;title !!}&lt;/a&gt;&lt;/h6&gt;
                                  &lt;!--&lt;i class="seoicon-clock"&gt;&lt;/i&gt;
                                   &lt;time class="published" datetime="2016-04-17 12:00:00"&gt;
                                      {{ $category-&gt;created_at-&gt;diffForHumans() }}
                                   &lt;/time&gt;--&gt;
                                  &lt;h6 style="width:300px; height:85px;"&gt;{!! str_limit($category-&gt;summary, 85) !!} 
                                      &lt;small&gt; &lt;a href="{{ route('post.single',$posttitle-&gt;slug) }}"&gt;Read More&lt;/a&gt;&lt;/small&gt;
                                  &lt;/h6&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          @endforeach
                      &lt;/div&gt;
                  &lt;/div&gt;
  
              &lt;div class="padded-50"&gt;&lt;/div&gt;
          &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and this is the single page view  below </p>
  
  <pre><code>    &lt;div class="content-wrapper"&gt; &lt;div class="container"&gt; &lt;div&gt;
  &lt;main class="main"&gt;
    &lt;div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-1 col-md-7 col-md-offset-1 col-lg-6 col-lg-offset-3 panel panel-reading"&gt;
        &lt;article class="hentry post post-standard-details"&gt;
          &lt;h2&gt;{!! $post-&gt;posttitle !!}&lt;/h2&gt;
          &lt;div class="post__content"&gt;
  
            &lt;div class="post-additional-info"&gt;
              &lt;div class="post__author author vcard"&gt;
                By:
                &lt;div class="post__author-name fn"&gt;
                  &lt;a href="#" class="post__author-link"&gt;{{ $post-&gt;user-&gt;username}}&lt;/a&gt;
                &lt;/div&gt;
              &lt;/div&gt;
  
              {{--Post date aka page date --}}
              &lt;span class="post__date"&gt;
                &lt;time class="published" datetime="2016-03-20 12:00:00"&gt;
                  &lt;i class="seoicon-clock"&gt;&lt;/i&gt; {{ $post-&gt;created_at-&gt;diffForHumans() }}
                &lt;/time&gt;
              &lt;/span&gt;
              {{--End Post date aka page date--}}
            &lt;/div&gt;
  
            {{--Post content aka page content--}}
            &lt;div class="post__content-info"&gt;
              &lt;p&gt; {!! $post-&gt;content !!} &lt;/p&gt;
            &lt;/div&gt;
            {{--End post content aka page content--}}
  
          &lt;/div&gt;
  
          &lt;div class="pagination-arrow"&gt;
          {{--Left arrow--}}
             @if($prev)
              &lt;a href="{{ route('post.single', ['slug' =&gt; $prev-&gt;slug] )}}" class="btn-prev-wrap"&gt;
                  &lt;svg class="btn-prev"&gt;
                    &lt;use xlink:href="#arrow-left"&gt;&lt;/use&gt;
                  &lt;/svg&gt;
                  &lt;div class="btn-content"&gt;
                    &lt;div class="btn-content-title"&gt;Previous Page&lt;/div&gt;
                      &lt;p class="btn-content-subtitle"&gt;{{ $prev-&gt;posttitle}}&lt;/p&gt;
                  &lt;/div&gt;
              &lt;/a&gt;
            @endif 
  
        {{--Right arrow--}}
           @if($next)
             &lt;a href="{{ route('post.single', ['slug'=&gt;$next-&gt;slug]) }}" class="btn-next-wrap"&gt;
               &lt;div class="btn-content"&gt;
                 &lt;div class="btn-content-title"&gt;Next Page&lt;/div&gt;
                   &lt;p class="btn-content-subtitle"&gt;{{ $next-&gt;posttitle }}&lt;/p&gt;
                &lt;/div&gt;
                &lt;svg class="btn-next"&gt;
                  &lt;use xlink:href="#arrow-right"&gt;&lt;/use&gt;
                &lt;/svg&gt;
              &lt;/a&gt;
                @endif
          &lt;/div&gt;
  
        &lt;/article&gt;
    &lt;/div&gt;
  &lt;/main&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>HERE'S the post database </p>
  
  <pre><code>    public function up()
  {
      Schema::create('posts', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('user_id');
          $table-&gt;integer('category_id');
          $table-&gt;string('posttitle');
          $table-&gt;string('slug');
          $table-&gt;text('content');
          $table-&gt;softDeletes();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>IN CONCLUSION:
  Using this how would you filter it so that only the post that has the same category_id is displayed when the user clicks on that category? </p>
  
  <p>Thank you 
  As a bonus I did a short screen recording of the problem <a href="https://www.screencast.com/t/vVzRMSunH" rel="nofollow noreferrer">https://www.screencast.com/t/vVzRMSunH</a></p>