Skip to content

Instantly share code, notes, and snippets.

@BataBoom
Last active January 6, 2024 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BataBoom/06942536e83c3ab05af3176760b7c903 to your computer and use it in GitHub Desktop.
Save BataBoom/06942536e83c3ab05af3176760b7c903 to your computer and use it in GitHub Desktop.
Livewire V3 Pagination w/ Multiple Models
<?php
namespace App\Http\Livewire\Paginate;
use Livewire\Component;
use Livewire\Attributes\Validate;
use Livewire\Attributes\Locked;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Collection;
use App\Models\Product;
use App\Models\Posts;
use App\Models\User;
use App\Models\Blogs;
use Livewire\Attributes\Renderless;
use Livewire\Attributes\Js;
use Livewire\WithPagination;
use Livewire\Attributes\Url;
use Livewire\Attributes\Computed;
class NestedPaginator extends Component
{
use WithPagination;
#[Locked]
public $userId;
#[Validate('required|min:3')]
#[Url]
public string $search = '';
public array $searchable = [];
public $searchHistory;
public bool $showHistory = false;
public bool $showFilters = false;
/*
Add this to AppServiceProvider:
```
Collection::macro('fancypaginate', function ($perPage = 15, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
```
*/
public function mount($uid)
{
$this->userId = $uid;
//$this->user = Auth::user();
$this->resetPage();
$this->searchHistory = array_slice($this->getCache(), 0, -1);
}
#[Computed(persist: true)]
public function user()
{
return User::find($this->userId);
}
public function updated($search)
{
$this->reset('results');
$this->validateOnly('search');
}
public function updated($searchHistory)
{
return $this->searchHistory = array_slice($this->getCache(), 0, -1);
}
public function resetForm()
{
$this->reset(['search', 'results']);
}
#[Computed(persist: false)]
public function searchPosts() {
if(strlen($this->search) >= 3) {
//if($this->validateOnly('search')) {
$this->setCache();
$posts = Posts::Where('type', 'LIKE', '%' . $this->search . '%')->get()->flatten()->fancypaginate(3, null, null, 'tweets-page');
$users = User::Where('name', 'LIKE', '%' . $this->search . '%')->get()->flatten()->fancypaginate(3, null, null, 'users-page');
$blogs = Blogs::Where('type', 'LIKE', '%' . $this->search . '%')->get()->flatten()->fancypaginate(3, null, null, 'blogs-page');
$products = Product::Where('name', 'LIKE', '%' . $this->search . '%')->where('enabled', 1)->get()->flatten()->fancypaginate(3, null, null, 'products-page');
$resultz = collect();
return $resultz->push($posts, $blogs, $users, $products);
} else {
return NULL;
}
/*
results will appear as $this->searchPosts
foreach it out to each instanceof and
@if($loop->last)
<div class="block pt-20"></div>
{{ $result->withQueryString()->links() }}
@endif
*/
}
//wire:keydown.enter="getResults"
public function getResults()
{
return $this->searchPosts();
}
public function updatingPaginators($page, $pageName)
{
if($pageName == 'users-page') {
$page = 'users-page';
} elseif($pageName == 'posts-page') {
$page = 'posts-page';
} elseif($pageName == 'blogs-page') {
$page = 'blogs-page';
} elseif($pageName == 'products-page') {
$page = 'products-page';
}
}
public function updatedPaginators($page, $pageName)
{
// Runs after the page is updated for this component...
}
public function clearHistory() {
Cache::forget('search.history_'.$this->user()->id);
return $this->searchHistory = [];
}
public function getCache() {
if(Cache::has('search.history_'.$this->user()->id)) {
return Cache::get('search.history_'.$this->user()->id);
} else {
return array();
}
}
private function setCache() {
$this->validateOnly('search');
$fetchCache = $this->getCache();
$git = array_unique($fetchCache);
$git[] = $this->search;
Cache::put('search.history_'.$this->user()->id, $git);
}
public function render()
{
return view('livewire.paginate.nested-paginator');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment