如何解决Laravel Livewire 分页在索引页后不显示搜索结果
我看过其他关于同一问题的帖子,但没有一个帖子反映了我的设置,这让我很难实施。
我使用的是 Laravel 8.0 和 Livewire 1.3
我目前有一个包含 186 个项目的“项目”列表,每页 25 个分页——这很好用。我也有实时搜索,如果在索引页面上,效果很好,但是如果您在索引后的任何页面上,例如 http://workorders.test/items?page=2
并决定进行搜索,它将显示正确数量的结果,但是不向页面显示结果(见附件截图)。
index.php
<?php
namespace App\Http\Livewire\Admin\Items;
use Livewire\Component;
use App\Item;
use Livewire\WithPagination;
class Index extends Component
{
use withPagination;
public $item;
public $item_name;
public $item_description;
public $perPage = 25;
public $sortAsc = true;
public $search = '';
public $sortField = 'item_name';
...
public function render()
{
$types = collect([
['name' => 'Equipment'],['name' => 'Supply'],]);
$plucked = $types->pluck('name')->all();
$items = Item::search($this->search)
->orderBy($this->sortField,$this->sortAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return view('livewire.admin.items.index',compact('items','plucked'));
}
}
item.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Item extends Model
{
use SoftDeletes;
protected $fillable = [
'item_name','item_description'
];
public function products()
{
return $this->hasMany(Product::class);
}
public static function search($query)
{
return empty($query) ? static::query()
: static::where('item_name','like','%'.$query.'%');
}
}
index.blade (剥离以仅显示重要部分)
<div class="flex-1 min-w-0 rounded-md shadow-sm">
<input wire:model="search" id="search" placeholder="Search Item Name">
</div>
...
@foreach($items as $item)
...
@endforeach
...
{{ $items->links() }}
解决方法
你错过了一些东西。当您使用这样的过滤器时,您需要告诉 livewire 重置页面。
在您的情况下,过滤器存储在 $search
变量中,因此只需将此方法添加到您的 Livewire 组件:
public function updatingSearch()
{
$this->resetPage();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。