PHP code example of ericksonreyes / pagination

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

    

ericksonreyes / pagination example snippets


namespace App\Http\Controllers;

use App\Repository\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;

class Users extends BaseController {

    private const DEFAULT_PAGE_SIZE = 35;

    public function index(Request $request, UserRepository $repository): Response {
        $page = (int) $request->get('page', 1);
        $size = (int) $request->get('size', self::DEFAULT_PAGE_SIZE);
        
        if ($page < 1) {
            $page = 1;
        }
        
        if ($size < 1) {
            $size = self::DEFAULT_PAGE_SIZE;
        }
        
        $offset = $page - 1;
        $limit = $size;
        $count = $repository->countUsers();
        $data['users'] = $repository->getUsers($offset, $limit);  
        
        $data['pagination'] = new Pagination(
            recordsFound: $count,
            recordsPerPage: 10,
            currentPage: $page
        );
        
        return response()->view('list', $data);
    }
    
}

@if(isset($pagination) && $pagination->hasPages())
    <ul class="pagination">
    
        @if($pagination->hasPreviousPage())
            <li>
                <a href="{{ route('records.list', ['page' => $pagination->previousPage()]) }}">
                    Previous
                </a>
            </li>
        @endif
                        
        @if($pagination->hasFirstPage())
            <li>
                <a href="{{ route('records.list', ['page' => $pagination->firstPage()]) }}">
                    {{ $pagination->firstPage() }}
                </a>
            </li>
            <li>...</li>
        @endif                    
        
        @foreach($pagination->pages() as $page)
            @if($pagination->currentPage() === $page)
                <li><span class="span--strong">{{ $page }}</span></li>
            @else
                <li>
                    <a href="{{ route('records.list', ['page' => $page]) }}">
                        {{ $page }}
                    </a>
                </li>
            @endif
        @endforeach
        
        @if($pagination->hasLastPage())
            <li>...</li>
            <li>
                <a href="{{ route('records.list', ['page' => $pagination->lastPage()]) }}">
                    {{ $pagination->lastPage() }}
                </a>
            </li>
        @endif   
        
        @if($pagination->hasNextPage())
            <li>
                <a href="{{ route('records.list', ['page' => $pagination->hasNextPage()]) }}">                
                    Next
                </a>
            </li>
        @endif             
    </ul>
@endif

 
    if(isset($pagination) && $pagination->hasPages()) {