XetaIO /
Xetaravel
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Xetaravel\Livewire\Traits; |
||
| 6 | |||
| 7 | use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
||
| 8 | use Illuminate\Database\Eloquent\Builder; |
||
| 9 | use Livewire\WithPagination; |
||
| 10 | use Psr\Container\ContainerExceptionInterface; |
||
| 11 | use Psr\Container\NotFoundExceptionInterface; |
||
| 12 | |||
| 13 | trait WithPerPagePagination |
||
| 14 | { |
||
| 15 | use WithPagination; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Assign the perPage option from the session. |
||
| 19 | * |
||
| 20 | * @return void |
||
| 21 | * |
||
| 22 | * @throws ContainerExceptionInterface |
||
| 23 | * @throws NotFoundExceptionInterface |
||
| 24 | */ |
||
| 25 | public function mountWithPerPagePagination(): void |
||
| 26 | { |
||
| 27 | $this->perPage = session()->get('perPage', $this->perPage); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Store in session the perPage option. |
||
| 32 | * |
||
| 33 | * @param mixed $value The value of the option perPage. |
||
| 34 | * |
||
| 35 | * @return void |
||
| 36 | */ |
||
| 37 | public function updatedPerPage(mixed $value): void |
||
| 38 | { |
||
| 39 | session()->put('perPage', $value); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Apply the pagination to the query. |
||
| 44 | * |
||
| 45 | * @param Builder $query The query to apply the pagination. |
||
| 46 | * |
||
| 47 | * @return LengthAwarePaginator |
||
| 48 | */ |
||
| 49 | public function applyPagination(Builder $query): LengthAwarePaginator |
||
| 50 | { |
||
| 51 | return $query->paginate($this->perPage); |
||
| 52 | } |
||
| 53 | } |
||
| 54 |