Skip to content

Instantly share code, notes, and snippets.

@veggigit
Last active October 8, 2021 13:06
Show Gist options
  • Save veggigit/18559cff769624ca7e8b82c405b29365 to your computer and use it in GitHub Desktop.
Save veggigit/18559cff769624ca7e8b82c405b29365 to your computer and use it in GitHub Desktop.
LIVEWIRE, paginate and display
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Gallery;
use Livewire\WithPagination;
class DisplayGallery extends Component
{
use WithPagination;
public $result;
public $pageNumber = 0;
public $hasMorePages;
public function mount()
{
$this->result = [];
$this->load();
}
public function render()
{
return view('livewire.display-gallery', ['gallery' => $this->result] );
}
public function load()
{
$this->pageNumber++;
$paginated = Gallery::paginate(20, ['*'], null, $this->pageNumber);
$this->hasMorePages = $paginated->hasMorePages();
$arrData = $paginated->toArray()['data'];
array_map(function($item){
array_push($this->result, $item);
}, $arrData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment