PHP code example of juampi92 / cursor-pagination

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;
}

$users = User::cursorPaginate(15);

$users = User::where('votes', '>', 100)->cursorPaginate(15);

$users = User::orderBy('id', 'desc')->cursorPaginate(15);

// 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(),
]);

cursorPaginate(array|int $perPage, array $cols = ['*'], array options = []): CursorPaginator;

 $resutls->hasMorePages(): bool;
 $results->nextCursor(): string|null;
 $results->prevCursor(): string|null;
 $results->previousPageUrl(): string|null;
 $results->nextPageUrl(): string|null;
 $results->url(['next' => 1]): string;
 $results->count(): int;
 

$following = $user->following()
    ->orderBy('follows.created_at', 'desc')
    ->cursorPaginate(10, ['*'], [
        'date_identifier' => true,
        'identifier' => 'follows.created_at',
        'identifier_alias' => 'created_at',
    ]);
`bash
php artisan vendor:publish --provider="Juampi92\CursorPagination\CursorPaginationServiceProvider" --tag="config"