AlsonicTech's avatar

Pagination with changing content

This is a known issue for every developer when using pagination. I did not found any simple solutions, except using something complicated like google pagination cursor.

We have the next scenario: Our pagination object has 5 items per page and 10 items in total

  1. Page 1 will display: item1, item2, item3, item4, item5
  2. We delete item4 and item5
  3. Page 2 will display: item8, item9, item10
  4. Issue: items6 and item7 disappears because they were moved in page 1

So the perfect scenario will be for Page 2 to display item6, item7, item8, item9 and item10

How can I accomplish this by using Laravel pagination, or anything else like a pagination library ?

0 likes
3 replies
moka's avatar

You'd have to listen for item deletes/creates and use JS to asynchronously update the pagination link with a URL variable that you could intercept and alter the database query.

jlrdw's avatar

Don't worry about such a thing just let pagination work if someone deleted so be it.

joedawson's avatar

You could use soft deletes. When you query your models, use the withTrashed() method and then when you render them in your views you could change the way they're displayed using the trashed() method on your model.

$items = Item::withTrashed()->get();
@foreach($items as $item)
    @if($item->trashed())
        <!-- Display a trashed / deleted item -->
    @else
        <!-- Display a normal item -->
    @endif
@endforeach

This will therefore not affect your pagination.

Please or to participate in this conversation.