unknown_'s avatar

Pagination - $i+1 not working

I added the paginate method to my code (5 posts per page), but when I go onto the next page it shows the numbers from 1 to 5, even though they should be from 6 to 10. This is my index.blade.php:

<table class="table table-bordered">
    <tr>
        <th>No</th>
        <th>Title</th>
        <th>Description</th>
        <th width="100px">Action</th>
    </tr>
       
    @foreach ($blogs as $i => $blog)
    <tr>
        <td>{{ $i+1 }}</td>
        <td>{{ $blog->title }}</td>
        <td>{{ $blog->description }}</td>
        <td>
        <a class="btn btn-primary" href="{{ route('blogs.show',$blog->id) }}">Show</a>
    </tr>
    @endforeach
</table>
0 likes
9 replies
Akash_kushwaha's avatar

You need to add pagination link in your blade file.

{{ $blogs->links() }}

unknown_'s avatar

@Akash_kit I have the link and it sends me onto the second page, but the id of the blogs stays the same, even though they should be numbered from 6 to 10 and not from 1 to 5 again

Sinnbeck's avatar

$i always starts with 0. It has nothing to do with your ids or page. If you want the offset you can do

<td>{{ $i+1 + (($blogs->currentPage() - 1) * $blogs->perPage()) }}</td>
1 like
unknown_'s avatar
unknown_
OP
Best Answer
Level 1

@Sinnbeck I deleted that code and put {{ $blog->id }} instead of it. thank you for your input!

Sinnbeck's avatar

@unknown_ ok so it wasn't the offset you wanted, but just the id? Be aware that they are not the same :)

Please or to participate in this conversation.