I'm creating a page that show a bunch of commodities this are shown on a livewire component. I have added pagination to it and works fine. Once i added searching it breaks when i start typing on the search box. I suspect that livewire is getting confused with the key after when presenting the new selection, if so how do i create a key that is unique to each element. Or is something wrong that i'm doing? Here is the code:
<?php
namespace App\Livewire\Usda;
use App\Models\UsdaCommodities;
use App\Models\UsdaMyList;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\Features\SupportPagination\WithoutUrlPagination;
use Livewire\WithPagination;
class Main extends Component
{
use WithPagination, WithoutUrlPagination;
#[Layout('layouts.app3')]
public $myLists;
public $myCommodities;
public $searchCommodity;
function mount()
{
$this->myLists = UsdaMyList::where('user_id', auth()->id())->get();
}
public function render()
{
return view('livewire.usda.main', [
'allCommodities' => $this->GetCommodities()
]);
}
#[Computed()]
function GetCommodities()
{
return UsdaCommodities::where('name', 'like', '%' . $this->searchCommodity . '%')
->orderBy('name')
->paginate(24);
}
}
@tykus using the reset page did not worked. Because i'm using WithiOutUrlPagination i don't think is necessary. The pagination works. The problem is when i type something on the search box, sometimes it works, but most times it locks. I fire up the devtools from Chrome and the console is showing this:
-> Detected mutiple instances of Alpine Running
-> Uncaught Snapshot missing on Livewire component with id: lFHhAr6soT6EH5J4leKb
->Uncaught (in promise) Component not found: lFHhAr6soT6EH5J4leKb
Could the multiple alpine running make a difference?
I had it working but is not the behavior that i wanted. I change the input from wire:model.live to wire:model.blur and added a reset page when the variable is updated:
function updatedSearchCommodity(): void
{
$this->resetPage();
}
i wanted it to be updating as i typed in the search box.