如何解决Livewire 分页问题?
<?php
namespace App\Http\Livewire;
use App\Models\Product;
use Livewire\Component;
use Livewire\WithPagination;
class ShowProducts extends Component
{
use WithPagination;
public $products =array();
public $type;
public function render()
{
return view('livewire.show-products');
}
public function updated()
{
$builder =Product::query();
if (!empty($this->type)) {
$builder->whereHas('type', function($query) {
$query->where('name', $this->type);
});
}
$this->products = $builder->get();
}
}
如果
$this->products = $builder->get();
换成
$this->products = $builder->paginate(15);
就会得到以下报错
Livewire\Exceptions\PublicPropertyTypeNotAllowedException
Livewire component's [show-products] public property [products] 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.
http://127.0.0.1:8000/livewire/message/show-products
官方教程也没看明白!
https://learnku.com/docs/laravel/8.x/pagination/9402
livewire
组件类中属性不支持分页对象,可以这么实现:数据集合直接通过render传递到前端页面,前端直接使用:
有别的实现也请大佬们指点