Paginate Does Not Account For Changing Results
I use paginate in Laravel to load a large amount of data. Data that changes quickly. The problem is that as the user scrolls through the data, new rows are being inserted at the top. This creates an issue where the user sees a duplicate row that got pushed out of page 1 into page 2 in the time between the user loading page 1 and page 2. For example:
Data:
- D
- E
- F
User A loads results using ->simplePaginate(2).
Page #1 loads:
Results:
- D
- E
inserts a row into the database.
Data:
- C
- D
- E
- F
User A now scrolls past his results on page #1 and page #2 loads:
Results:
- E
- F
User A now just saw:
- D
- E
- E <-- error
- F
Is there a way I can easily account for this when loading the second page?
User A should have seen
- D
- E
- F
It would also be super cool to pass along some information when User A loads page #2 saying "Hey, there are new results at the top now."
Answer
Take a timestamp on page 1, and then add an additional clause to the query, asserting that created_at
is before the timestamp.
$beforeTime = $request->input('before_time', Carbon::now());
$items = Item::where('created_at', '<=', $beforeTime)->paginate(2);
Lastly, make sure to append the before_time
parameter to the pagination links:
echo $items->appends(['before_time' => $beforeTime])->links();
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms