Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mlazuardy's avatar

Make Paginate in Show Method

How to add pagination inside Show Function? i have blog item that have comment and latest article in sidebar, how to use paginate inside it?

0 likes
39 replies
ftrillo's avatar

You can pass any ammount of variables to the view.

$blog = Blog::findOrFail($id);
$data = [
    'blog' => $blog,
    'paginatedComments' => ''/*Get the paginated comments*/,
    'lastArticle' => ''/*Get the last article*/
];
return response()->view('myView', $data, 200);

Then you can reference those three variables in the view.

If you don't know how to paginate queries in Laravel you should read this. https://laravel.com/docs/5.5/pagination

mlazuardy's avatar

this is my show.blade.php in Post view

  @foreach ($post->comments as $comment)
                      <li class="media">
                          <div class="media-left">
                              <a href="#">
                                  <img class="media-object" src="img/ardi.png" width="70" height="70" alt="...">
                              </a>
                          </div>
                          <div class="media-body">

                              <h4 class="media-heading">{{$comment->name}}</h4>
                            <p>{{$comment->body}}</p>

                          </div>
                      </li>
                    @endforeach

and the show method in PostController.php is

  public function show ($slug)
    {

      $posts = Post::where('slug',$slug)->first();
      if(!$posts){
        abort(404);}
      return view('post.show')->with('post',$posts);
    }

what code i need to change?

Snapey's avatar

Do you want to paginate posts or comments?

also, this;

      $posts = Post::where('slug',$slug)->first();
      if(!$posts){
        abort(404);}

You can do with firstOrFail()

      $posts = Post::where('slug',$slug)->firstOrFail(); 

and you should eager load the comments to reduce the number of queries;

      $posts = Post::with('comments')->where('slug',$slug)->firstOrFail(); 

mlazuardy's avatar

i want to paginate comments sir, so how to do that? because when i insert $post->comment->link() . it doesnt work

ftrillo's avatar
public function show($slug)
{
    $post = Post::where('slug',$slug)->firstOrFail();
    $commentPaginator = $post->comments()->paginate(10);
    return view('post.show')->with(['post' => $post, 'commentPaginator' => $commentPaginator]);
    // shorter way
    // return view('post.show')->with(compact('post', 'commentPaginator'));
}

In the view:

 @foreach ($commentPaginator as $comment)
                      <li class="media">
                          <div class="media-left">
                              <a href="#">
                                  <img class="media-object" src="img/ardi.png" width="70" height="70" alt="...">
                              </a>
                          </div>
                          <div class="media-body">

                              <h4 class="media-heading">{{$comment->name}}</h4>
                            <p>{{$comment->body}}</p>

                          </div>
                      </li>
                    @endforeach 
{{ $commentPaginator->links() }}
2 likes
ftrillo's avatar

Well, It should display the correct page automatically.

If the post has 10 or less comments you're only going to see comments in page 1.

Are you manually typing the url or do the buttons to switch page actually appear?

mlazuardy's avatar

oh i see it, i just have 4 comment , but just want to test the pagination, so i change the pagination to

  $commentPaginator = $post->comments()->paginate(2);

so the summary is, i cant make paginate with comment less than 10? event i paginated it?

Snapey's avatar

pagination links should not appear if there is only one page

mlazuardy's avatar

it appear sir, but the links didnt load anything, just change the url because i want to testing the pagination link so i change the paginate(10) to paginate(2)

ftrillo's avatar

The number you pass to the paginate() method is the number of comments that are going to be displayed in each page.

mlazuardy's avatar

yes sir but the problem is , the links on pagination didnt work. it won't load the next comment , i already have 11 comment and paginating 10. but when i hover and click the next comment , it just load the page url, not the next comment page

ftrillo's avatar

If you can see a button that takes you to page 2. Then page 2 should contain at least one comment.

Anything but that, puzzles me. I don't know what we're missing.

mlazuardy's avatar

it show the button sir with 2 number like << 1 2 >> but when i press 2 , it just change the url, but not load the comment and the page

Snapey's avatar

Why does your URL not contain the post ID?

Snapey's avatar

when you load page 2, how does the post get loaded when you are not passing post id?

mlazuardy's avatar

i dont know sir, i just click the <2> link, and it wont load anything ,but just the url like

localhost:8000/blog/testing?page=2
but the second commend page didnt show
Snapey's avatar

ok, let's make it simpler. On page 1, how is the post id passed ?

mlazuardy's avatar

its just localhost:8000/blog/testing while testing is my blog post name, so click on page 2 pagination, its show localhost:8000/blog/testing?page=2

but wont load anything, just the url. in order to work, i must refresh my browser, and the page is loaded,

clicking again to page 1, and show localhost:8000/blog/testing?page=1

but same like page 2, it wont load anything except the url

so i must refresh again my browser

ftrillo's avatar

Could you show the routes file? Specifically the definition of that route.

Snapey's avatar

And show the complete controller show method

mlazuardy's avatar

show method on PostController.php


    public function show ($slug)
    {

      $post = Post::where('slug',$slug)->firstOrFail();
      $commentPaginator = $post->comments()->paginate(10);
      if(!$post){
        abort(404);}
      //return view('post.show')->with('post',$posts);
      return view('post.show')->with(['post'=>$post,'commentPaginator'=>$commentPaginator]);
    }

all blog route

Route::get('/blog', 'PostController@index');
Route::get('/blog/{slug}','PostController@show');
Route::post('/blog/{post}/comments','CommentController@store');

Route::get('/tags/{tag}','TagController@index');
Snapey's avatar

Can you try it without loading through the relationship?

    public function show ($slug)
    {

      $post = Post::where('slug',$slug)->firstOrFail();

      $commentPaginator = Comment::where('post_id',$post->id)->paginate(10);
      return view('post.show')
        ->with(['post'=>$post,'commentPaginator'=>$commentPaginator]);
    }
mlazuardy's avatar

still doesnt work sir

this make me made but i need to know for my knowledge, because when i deploy my site, maybe i just use disquss

ftrillo's avatar

When you go to page 2. Do you still see the post in your view? Do you get no comments, or the same comments that are in page 1? Are you getting any kind of exception?

mlazuardy's avatar

I get the same comments that are in page 1 sir

Snapey's avatar

When you reply, mention the person so that they see a bell next to their icon

jlrdw's avatar

Have you tested your logic? Paginate your data in a view only just to make sure you are paginating correctly. If it's not correct in a view, it will never be correct elsewhere.

Next

Please or to participate in this conversation.