Skip to content

[12.x] create a Pagination component #56295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Illuminate/Pagination/Pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Illuminate\Pagination;

use Closure;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Pagination extends Component
{
/**
* Create a new component instance.
*/
public function __construct(
protected LengthAwarePaginator|Paginator $paginator,
protected ?string $view = null,
protected array $data = [],
) {
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
//length aware paginator
if ($this->paginator instanceof LengthAwarePaginator) {
//get elements
$window = UrlWindow::make($this->paginator);

$elements = array_filter([
$window['first'],
is_array($window['slider']) ? '...' : null,
$window['slider'],
is_array($window['last']) ? '...' : null,
$window['last'],
]);
}

//determine view
$view = $this->view ?? $this->paginator instanceof LengthAwarePaginator
? AbstractPaginator::$defaultView
: AbstractPaginator::$defaultSimpleView;

//load view
return view($view)
->with([
...$this->data,
'paginator' => $this->paginator,
'elements' => $elements ?? [],
]);
}
}
3 changes: 3 additions & 0 deletions src/Illuminate/Pagination/PaginationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Pagination;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class PaginationServiceProvider extends ServiceProvider
Expand All @@ -15,6 +16,8 @@ public function boot()
{
$this->loadViewsFrom(__DIR__.'/resources/views', 'pagination');

Blade::component('laravel::pagination', Pagination::class);

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/pagination'),
Expand Down
Loading