Make Paginate in Show Method

Published 2 months ago by mlazuardy

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?

ftrillo

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

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
Snapey
2 months ago (707,405 XP)

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

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

ftrillo
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() }}
mlazuardy

let me try

mlazuardy

it works sir, but i have problem again, the page to the next paginated comment url is

http://localhost:8000/blog/testing?page=2

but my page didnt load anything , just the url change.

i must return to

http://localhost:8000/blog/testing?page=1

to make the pagination works, how to fix it? sorry bad english

ftrillo

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

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
Snapey
2 months ago (707,405 XP)

pagination links should not appear if there is only one page

mlazuardy

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

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

mlazuardy

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

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

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

Please sign in or create an account to participate in this conversation.