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