Skip to content

Commit

Permalink
Create WithPaginateScroll.php
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 authored Nov 1, 2024
1 parent e948929 commit b0b6225
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/Models/Concerns/WithPaginateScroll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Foxws\WireUse\Models\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
use Laravel\Scout\Scout;
use Livewire\WithPagination;

trait WithPaginateScroll
{
use WithQueryBuilder;
use WithPagination;
use WithScroll;

public function bootWithPaginateScroll(): void
{
throw_if(
! method_exists($this, 'getBuilder') ||
! ($this->getBuilder() instanceof Builder || ! $this->getBuilder() instanceof Scout)
);
}

public function updatedPage(): void
{
$this->fetch();
}

public function isFetchable(): bool
{
if ($this->items->isEmpty()) {
return true;
}

return $this->getBuilder()->hasMorePages();
}

public function clear(): void
{
$this->reset('fetchCount');

$this->resetPage();

$this->models = collect();

unset($this->items);
}

protected function getMergeCandidates(): Collection
{
return $this->getBuilder()->getCollection();
}

protected function getBuilder(): Paginator
{
return $this->getQuery()
->simplePaginate(
perPage: $this->getCandidatesLimit(),
page: $this->getPage(),
);
}
}

0 comments on commit b0b6225

Please sign in to comment.