如何使用 laravel livewire 对 http 响应的结果进行分页
How paginate result of http response with laravel livewire
我想用调用 HTTP 请求填充 table 并获取 JSON 很好,
我这样设置火线可以正确填充 table:
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
解决方法:
需要将数组转换为集合,然后创建一个宏以在
上使用分页
collection.
public function render()
{
$this->response = Http::timeout(30)->get('http://127.0.0.1:8000/api/lines')->body();
$collection = collect(json_decode($this->response));
return view('livewire.line-index', ['lines' =>$collection->paginate(20)])->layout('layouts.app', ['header' => 'Line Management']);
}
要创建宏,您需要更新 AppServiceProvider.php 文件:
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
/**
* Paginate a standard Laravel Collection.
*
* @param int $perPage
* @param int $total
* @param int $page
* @param string $pageName
* @return array
*/
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
}
}
参考:https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e
我想用调用 HTTP 请求填充 table 并获取 JSON 很好, 我这样设置火线可以正确填充 table:
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
解决方法: 需要将数组转换为集合,然后创建一个宏以在
上使用分页collection.
public function render()
{
$this->response = Http::timeout(30)->get('http://127.0.0.1:8000/api/lines')->body();
$collection = collect(json_decode($this->response));
return view('livewire.line-index', ['lines' =>$collection->paginate(20)])->layout('layouts.app', ['header' => 'Line Management']);
}
要创建宏,您需要更新 AppServiceProvider.php 文件:
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
/**
* Paginate a standard Laravel Collection.
*
* @param int $perPage
* @param int $total
* @param int $page
* @param string $pageName
* @return array
*/
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
}
}
参考:https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e