【发布时间】:2021-03-25 20:49:18
【问题描述】:
我想用调用 HTTP 请求来填充一个表并获取一个 JSON 罚款, 我像这样设置了可以正确填充表格的火线:
public function render()
{
$this->response = Http::timeout(30)->get('http://127.0.0.1:8000/api/lines')->body();
return view('livewire.line-index', ['lines' =>json_decode($this->response)])->layout('layouts.app', ['header' => 'Line Management']);
}
但是当我像这样添加分页时:
public function render()
{
$this->response = Http::timeout(30)->get('http://127.0.0.1:8000/api/lines')->body();
return view('livewire.line-index', ['lines' =>json_decode($this->response)->paginate(25)])->layout('layouts.app', ['header' => 'Line Management']);
}
我看到了这个错误:
Call to a member function paginate() on array
【问题讨论】:
-
paginate()必须在查询构建器实例上调用,而不是“任何东西”。您可以为集合上的分页器创建一个宏,然后在分页之前将响应转换为集合。 -
@Qirel 我将它转换为集合,但仍然无法对集合进行分页
$collection = collect(json_decode($this->response)); return view('livewire.line-index', ['lines' =>$collection->paginate(25)])->layout('layouts.app', ['header' => 'Line Management']);但仍然看到这个Method Illuminate\Support\Collection::paginate does not exist. -
...但要做到这一点,您必须创建一个对集合进行分页的宏,就像我说的那样。
-
@Qirel 谢谢。它在这里作为参考如何创建宏
https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e
标签: php laravel pagination laravel-8 laravel-livewire