1. Go to this page and download the library: Download lampager/lampager 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/ */
lampager / lampager example snippets
use Lampager\Paginator;
use Lampager\ArrayProcessor;
$cursor = [
'id' => 3,
'created_at' => '2017-01-10 00:00:00',
'updated_at' => '2017-01-20 00:00:00',
];
$query = (new Paginator())
->forward()
->limit(5)
->orderByDesc('updated_at') // ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
->orderByDesc('created_at')
->orderByDesc('id')
->seekable()
->configure($cursor);
$rows = run_your_query_using_PDO($query); // Note: SQLite3 driver example is bundled in the tests/StubPaginator.php. Please refer to that.
$result = (new ArrayProcessor())->process($query, $rows);
use Lampager\Query;
use Lampager\ArrayProcessor;
use Lampager\PaginationResult;
$formatter = function ($rows, array $meta, Query $query) {
// Drop table prefix in meta properties (e.g. "posts.updated_at" -> "updated_at")
foreach (array_filter($meta, 'is_array') as $property => $cursor) {
foreach ($cursor as $column => $field) {
unset($meta[$property][$column]);
$segments = explode('.', $column);
$meta[$property][end($segments)] = $field;
}
}
return new PaginationResult($rows, $meta);
};
$result = (new ArrayProcessor())->useFormatter($formatter)->process($query, $rows);
use Lampager\Query;
use Lampager\ArrayProcessor;
use Lampager\PaginationResult;
use Lampager\Contracts\Formatter;
class DropTablePrefix implements Formatter
{
public function format($rows, array $meta, Query $query)
{
// Drop table prefix in meta properties (e.g. "posts.updated_at" -> "updated_at")
foreach (array_filter($meta, 'is_array') as $property => $cursor) {
foreach ($cursor as $column => $field) {
unset($meta[$property][$column]);
$segments = explode('.', $column);
$meta[$property][end($segments)] = $field;
}
}
return new PaginationResult($rows, $meta);
}
}
$result = (new ArrayProcessor())->useFormatter(DropTablePrefix::class)->process($query, $rows);
use Illuminate\Database\Eloquent\Builder;
use Lampager\Query;
use Lampager\Laravel\Processor as IlluminateProcessor;
IlluminateProcessor::setDefaultFormatter(function ($rows, array $meta, Query $query) {
// Note:
// $builder is provided from extended Paginator.
// For example, lampager/lampager-laravel provides QueryBuilder, EloquentBuilder or Relation.
$builder = $query->builder();
switch ($builder instanceof Builder ? $builder->getModel() : null) {
case Post::class:
return (new PostFormatter())->format($rows, $meta, $query);
case Comment::class:
return (new CommentFormatter())->format($rows, $meta, $query);
default:
return new PaginationResult($rows, $meta);
}
});
$posts = Post::lampager()->orderBy('created_at')->orderBy('id')->paginate();
$comments = Comment::lampager()->orderBy('created_at')->orderBy('id')->paginate();
sql
(
SELECT * FROM `posts`
WHERE `user_id` = 1
AND (
`updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` > 3
OR
`updated_at` = '2017-01-20 00:00:00' AND `created_at` > '2017-01-10 00:00:00'
OR
`updated_at` > '2017-01-20 00:00:00'
)
ORDER BY `updated_at` ASC, `created_at` ASC, `id` ASC
LIMIT 1
) UNION ALL (
SELECT * FROM `posts`
WHERE `user_id` = 1
AND (
`updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` <= 3
OR
`updated_at` = '2017-01-20 00:00:00' AND `created_at` < '2017-01-10 00:00:00'
OR
`updated_at` < '2017-01-20 00:00:00'
)
ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
LIMIT 6
)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.