【发布时间】:2021-05-11 10:36:15
【问题描述】:
我试图在页面装载时检索用户并使用以下代码对其进行分页:
public function mount()
{
$this->users = User::where('register_completed', '1')->paginate(16);
}
但我收到此错误:
Livewire component's [user-search] public property [users] must be of type: [numeric, string, array, null, or boolean]. Only protected or private properties can be set as other types because JavaScript doesn't need to access them.
计划是在页面加载时使用mount 加载所有用户,然后让用户使用具有多个条件的过滤器表单过滤它们。分页器使用以下代码工作:
public function render()
{
return view('livewire.user-search', [
'users' => User::where('register_completed', '1')->paginate(16),
])
->extends('layouts.app')
->section('content');
}
但我需要使用特定函数来根据所选条件过滤结果。此外,搜索不是实时的,并且有一个按钮可以调用搜索过滤器功能。
不知道为什么分页只在通过 render 方法时才有效。
$users 也是一个公共属性,可以从视图中访问它。
【问题讨论】:
-
请分享您完整的
UserSearchLivewire 组件。 -
这里是
<?php namespace App\Http\Livewire; use App\Models\User; use Livewire\Component; use Livewire\WithPagination; class UserSearch extends Component { use WithPagination; protected $paginationTheme = 'bootstrap'; public $search; public $users; public function render() { return view('livewire.user-search') ->extends('layouts.app') ->section('content'); } public function mount() { $this->users = User::where('register_completed', '1')->paginate(16); } }