1. Go to this page and download the library: Download juampi92/cursor-pagination library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
juampi92 / cursor-pagination example snippets
public function index()
{
$users = DB::table('users')->cursorPaginate();
return $users;
}
// Will use Booking's primaryKey
Bookings::cursorPaginate(10);
// Will use hardcoded 'id'
DB::table('bookings')->cursorPaginate(10);
// Will use 'created_by'
Bookings::orderBy('created_by', 'asc')
->cursorPaginate(10);
// Will use _id, ignoring everything else.
Bookings::cursorPaginate(10, ['*'], [
'identifier' => '_id'
]);
// It will autodetect 'datetime' as identifier,
// and will detect it's casted to datetime.
Bookings::orderBy('datetime', 'asc')
->cursorPaginate(10);
// It will autodetect 'datetime' as identifier,
// but since there is no model, you'll have to
// specify the 'date_identifier' option to `true`
DB::table('bookings')
->orderBy('datetime', 'asc')
->cursorPaginate(10, ['*'], [
'date_identifier' => true
]);
Route::get('api/v1', function () {
return App\User::cursorPaginate();
});
new CursorPaginator(array|collection $items, array|int $perPage, array options = [
// Attribute used for choosing the cursor. Used primaryKey on Eloquent Models as default.
'identifier' => 'id',
'identifier_alias' => 'id',
'date_identifier' => false,
'path' => request()->path(),
]);