PHP code example of juniora / laravel-redis-paginator

1. Go to this page and download the library: Download juniora/laravel-redis-paginator 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/ */

    

juniora / laravel-redis-paginator example snippets


$usersAsc = $redisPaginator->sortAsc()->paginate('leaderboard');

$usersDesc = $redisPaginator->sortDesc()->paginate('leaderboard');

public function show(User $user, LaravelRedisPaginator $redisPaginator)
{
    $memberRank = $redisPaginator->rank('user:' . $user->id, 'leaderboard');

    dump($memberRank->score, $memberRank->rank, $memberRank->page);
}

public function index()
{
    $users = RedisPaginator::perPage(25)->paginate('leaderboard');

    return view('leaderboard', compact('users'));
}

// Using the fluent interface
$users = $redisPaginator->page(5)->paginate('leaderboard');

// Using method parameters
$users = $redisPaginator->paginate('leaderboard', 'page', 5);

// https://www.example.com/leaderboard?page=5
$users = $redisPaginator->paginate('leaderboard');



namespace App\RedisResolvers;

use App\Models\User;
use GeTracker\LaravelRedisPaginator\Resolvers\AbstractResolver;

class UserResolver extends AbstractResolver
{
    // Defaults shown below, can be omitted
    protected $modelKey = 'id';

    protected $scoreField = 'score';

    /**
     * Load Eloquent models
     */
    protected function resolveModels(array $keys)
    {
        return User::findMany($keys);
    }

    /**
     * Resolve a key from Redis to an Eloquent incrementing ID or UUID
     */
    protected function resolveKey($key)
    {
        return (int) str_replace('user:', '', $key);
    }
}

$users = $this->paginator
    ->setModelResolver(new \App\RedisResolvers\UserResolver())
    ->paginate('leaderboard');

echo $users[0]->name.' -> '.$users[0]->score;
 php
public function index(LaravelRedisPaginator $redisPaginator)
{
    $users = $redisPaginator->perPage(25)->paginate('leaderboard');

    return view('leaderboard', compact('users'));
}