【问题标题】:How paginate result of http response with laravel livewire如何使用 laravel livewire 对 http 响应的结果进行分页
【发布时间】: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


【解决方案1】:

解决方案: 需要将数组转换为集合,然后创建一个宏来使用分页

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

【讨论】:

    猜你喜欢
    • 2015-09-24
    • 2021-12-04
    • 2020-07-19
    • 1970-01-01
    • 2021-09-09
    • 2017-07-02
    • 2019-06-24
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多