1

Controller function for showing all the posts on home page (works fine, I can click through and see all posts listed as expected):

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class PostsController extends Controller
{

    public function showPosts()
    {
        $posts = Post::paginate(5);
        return view('index', ['posts' => $posts]);
    }

Controller function for showing all posts on other pages:

use App\Page;
use App\Post;
use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function show($slug)
    {
        $posts = Post::paginate(16);
        $page = Page::findBySlug($slug);
        return view('page', ['page' => $page, 'posts' => $posts]);
    }

To display on the index I have inside index.blade:

    @foreach ($posts as $post)
    @include('partials.post', ['post' => $post])
    @endforeach

To display on the other pages I have inside pages.blade:

    {{ $posts->links() }}

    @foreach ($posts as $post)

    <?php
    {
        if ($post->category_id == $page->id){?>
            @include('partials.post', ['post' => $post])
        <?php  }
    }?>
    @endforeach

The home page works fine. The other pages work to a degree. They shows the correct posts on the correct pages. The issue is with the pagination.

Using the 16th post as an example, if I say $posts = Post::paginate(16); then the post display on the page and there is no pagination buttons shown as the total number of posts is 16. If I say $posts = Post::paginate(2); then the 16th post appears on pagination page 8, even when it is the only post for that page.

Dan
  • 951
  • 1
  • 23
  • 46
  • You can make your own pagination template and control buttons' appearance. – shukshin.ivan Apr 11 '18 at 22:15
  • I think this is a problem with the way I am getting the `$post` data rather than a problem with laravel's paginate, it was just an unexpected result I am not sure how to address. – Dan Apr 11 '18 at 22:21
  • It's unclear, what's wrong. "then the 16th post appears on pagination page 8, even when it is the only post for that page" - how come you have the only post? You have 16 posts! – shukshin.ivan Apr 11 '18 at 22:55

1 Answers1

1

I think you understand paginator wrong. You should paginate filtered results. Not filter paginated ones.

PagesController:

$page = Page::findBySlug($slug);
$posts = Post::where('category_id', $page->id)->paginate(16);
shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
  • 1
    That fixed my problem. To answer your previous comment. I have 16 posts in total. But the posts intended to be for specific pages based on matches with category id and page id. So in the case of post 16 it was only relevant to one page. I guess you figured that with the answer you gave me. Either way it works and I am very grateful thank you. – Dan Apr 11 '18 at 23:52
  • OK. My message is that you can't filter pagination without breaking it. You should filter first, then paginate. – shukshin.ivan Apr 12 '18 at 09:44