generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e948929
commit b0b6225
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
} | ||
} |