I have a controller where I pass the value of a search input to update a table of values in my view.
The table is beeing paginated to 5 on each page.
It seems that when I search something while I am on page 1, that everything works well, but when I am on another page than 1 search doesnt show me any result, but pagination still shows up in my view.
This is a very strange behaviour and I am not sure if this is a Livewire bug or that I am just missing something at all.
I am on the latest version of Livewire and already tried out the older v0.5.2.
Controller:
<?php
namespace App\Http\Livewire\Tags;
use App\Tag;
use Exception;
use Illuminate\Contracts\View\Factory;
use Illuminate\View\View;
use Livewire\Component;
use Livewire\WithPagination;
class TagEdit extends Component
{
use WithPagination;
public $searchInput;
protected $searchList;
protected $pagination = 5;
/**
* @param $id
* @throws Exception
*/
public function deleteTags($id): void
{
$deleteID = Tag::findOrFail($id);
$deleteID->delete();
}
/**
* @return Factory|View
*/
public function render()
{
$this->searchList = Tag::where('schlagwort', 'like', '%' . $this->searchInput . '%')
->orderBy('created_at', 'desc')
->paginate($this->pagination);
return view('livewire.tags.tag-edit', [
'searchList' => $this->searchList,
]);
}
}
View:
<div>
<div class="text-left mb-5">
<label for="labelSchlagwort" class="font-weight-bold">Schlagwort Suchen:</label>
<input class="form-control" placeholder="Schlagwort Suchen" type="text" wire:model="searchInput">
</div>
@if($searchList->total() > 0)
{{ $searchList->links() }}
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col" class="text-left">Schlagwort</th>
<th scope="col" class="text-left">Angelegt am</th>
<th scope="col" class="text-left"></th>
</tr>
</thead>
<tbody>
@foreach($searchList as $searchedItem)
<tr>
<td class="text-left">{{ $searchedItem->schlagwort }}</td>
<td class="text-left">{{ $searchedItem->created_at }}</td>
<td class="text-left">
<button class="btn btn-success">Editieren</button>
<button class="btn btn-danger" wire:click="deleteTags({{ $searchedItem->id }})">Löschen</button>
</td>
</tr>
@endforeach
</tbody>
</table>
{{ $searchList->links() }}
@endif
</div>
Video: