PHP 8.6上的Livewire 4搜索列表,可以使用WithPagination处理页码,并在搜索词变化时resetPage,避免用户停留在旧的高页码而看到空结果。下面适用于已有Laravel、Livewire 4及Post模型的项目,示例假定posts表有title字段,展示的是组件搜索和分页协作,不是新的PHP 8.6语言特性。
先检查实际依赖环境
1、运行php -v、composer show livewire/livewire和composer check-platform-reqs,确认web服务与命令行使用的php及扩展一致。livewire 4.4.9声明php ^8.1,但该约束不代替整个项目依赖的兼容检查。

Livewire分页需要WithPagination trait
2、在现有Livewire 4单文件组件中加入以下内容,组件所在路由应沿用项目原有访问权限。不要为了测试搜索而公开原本受控的数据。
搜索变化后回到第一页
<?php
use App\Models\Post;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Livewire\WithPagination;
new class extends Component {
use WithPagination;
public string $search = '';
public function updatedSearch(): void { $this->resetPage(); }
#[Computed]
public function posts() {
return Post::query()
->when($this->search !== '', fn ($q) =>
$q->where('title', 'like', '%' . $this->search . '%'))
->orderBy('id')->paginate(10);
}
};
?>
<div>
<input type="search" wire:model.live.debounce.300ms="search" placeholder="搜索标题">
@foreach ($this->posts as $post)
<p wire:key="post-{{ $post->id }}">{{ $post->title }}</p>
@endforeach
{{ $this->posts->links() }}
</div>
搜索条件变化后resetPage的官方示例
核对空结果和多个分页器
1、先进入第二页或更后页,再修改搜索词,确认回到第一页,结果按新的条件显示。无匹配时应确实为空,不能用旧数据回填假装搜索成功。LIKE中的%和_仍有通配符含义,需要字面搜索时另行按数据库规则转义。
使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。
2、同页有多个分页器时,为它们分别设置pageName,并在resetPage时传入相同名字。登录用户或租户数据还要在查询中加入对应范围,分页trait本身不会自动决定哪些记录可见。


















