【问题标题】:is there a Laravel pagination like this?有这样的Laravel分页吗?
【发布时间】:2018-10-01 20:55:39
【问题描述】:

我在 Laravel 框架分页中有一个非常具体的案例。

想象一下,我通过传递偏移量和限制参数从 Redis API 获得结果。换句话说,分页是在 API 端完成的。 现在,当我在 Laravel 应用程序中获得结果时,我想在分页中显示它们。我的意思是一个简单的分页视图,它提供到其他页面的导航。例如,第二个页面意味着我必须向我的 Redis API 发送请求才能获取第二组数据。

根据我对 Laravel 分页器类的理解,它需要一个项目集合并为它们提供方便的分页。和我想要的有点不同。

我只需要一个类来制作分页视图,它将项目总数作为参数并进行适当的链接布局。

在 Laravel 中有没有方便的方法来做到这一点? 要么 自己实现是我唯一的选择吗?

【问题讨论】:

  • LengthAwarePaginator 在“手动创建分页器”下的laravel.com/docs/5.6/pagination#displaying-pagination-results 中有更多详细信息
  • 感谢您,我已阅读该文档,但它并不能满足我的需要。它获取项目和其他选项的集合作为参数,并对它们进行分页
  • 如果你只需要链接,可以给它一个你需要的长度的账单集合。
  • 收单是什么意思?我不明白。你的意思是假收藏吗?但是怎么做?

标签: php laravel api pagination


【解决方案1】:

我正在使用下面的类来使用数据在分页中获取数据:-

namespace App\Helpers;

use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Countable;
use ArrayAccess;
use ArrayIterator;
use JsonSerializable;
use IteratorAggregate;
use Illuminate\Pagination\LengthAwarePaginator;

class LengthAwareOffsetPaginator extends  LengthAwarePaginator implements
Arrayable,
ArrayAccess,
Countable,
IteratorAggregate,
JsonSerializable,
Jsonable
{
    protected $items;

    protected $total;

    protected $total_pages;

    protected $limit;

    protected $offset;

    protected $options;

    /**
     * LengthAwareOffsetPaginator constructor.
     *
     * @param Collection $items
     * @param $total
     * @param $limit
     * @param $offset
     * @param array $options
     */
    public function __construct(Collection $items, $total, $limit, $offset, array $options = [])
    {
        $this->items = $items;

        if ($items->count() > $limit) {
            $this->items = $items->take($limit);
        }

        $this->total = $total;

        $this->limit = $limit;
        $this->offset = $offset;
        $this->options = $options;

        $this->total_pages = ($total/$limit);
    }

    /**
     * Get url of an offset.
     *
     * @param int $offset
     *
     * @return string Url of an offset
     */
    public function url($pageNumber)
    {
        $query = isset($this->options['queryParameter']) ? $this->options['queryParameter'] : [];

        $offset = ($pageNumber - 1) * $this->limit;
        $query = array_merge($query, ['page' => ['limit' => $this->limit, 'offset' => $offset]]);
        $url = isset($this->options['path']) ? $this->options['path'] : '/';

        return $url.'?'.http_build_query($query);
    }

    /**
     * Get last page.
     *
     * @return int Last page
     */
    public function lastPage()
    {
        $totalPages = ceil($this->total / $this->limit);
        return $totalPages;
    }

    /**
     * Get last page offset.
     *
     * @return int Last page offset
     */
    public function totalPages()
    {
        return $this->total_pages;
    }

    /**
     * Get current page.
     *
     * @return int Last page offset
     */
    public function currentPage()
    {
        $pages = (int)ceil($this->offset / $this->limit);

        $currentPage = ($pages + 1);

        return $currentPage;
    }

    public function perPage()
    {
        return $this->limit;
    }

    /**
     * Get last page url.
     *
     * @return string
     */
    public function lastPageUrl()
    {
        $last = $this->lastPage();

        return $this->url($last);
    }

    /**
     * get next page url.
     *
     * @return string
     */
    public function nextPageUrl()
    {
        $nextOffset = $this->offset + $this->limit;

        return ($nextOffset >= $this->total)
            ? null
            : $this->url($nextOffset);
    }

    /**
     * get previous page url.
     *
     * @return string
     */
    public function previousPageUrl()
    {
        if ($this->offset == 0) {
            return null;
        }

        $prevOffset = $this->offset - $this->limit;

        return ($prevOffset < 0)
            ? $this->url($prevOffset + $this->limit - $this->offset)
            : $this->url($prevOffset);
    }

    public function items()
    {
        return $this->items;
    }

    /**
     * get total items.
     *
     * @return int
     */
    public function total()
    {
        return $this->total;
    }

    /**
     * Get the number of items for the current page.
     *
     * @return int
     */
    public function count()
    {
        // return $this->total;
        return $this->items->count();
    }

    /**
     * Get an iterator for the items.
     *
     * @return \ArrayIterator
     */
    public function getIterator()
    {
        return new ArrayIterator($this->items->all());
    }

    /**
     * Determine if the given item exists.
     *
     * @param mixed $key
     *
     * @return bool
     */
    public function offsetExists($key)
    {
        return $this->items->has($key);
    }

    /**
     * Get the item at the given offset.
     *
     * @param mixed $key
     *
     * @return mixed
     */
    public function offsetGet($key)
    {
        return $this->items->get($key);
    }

    /**
     * Set the item at the given offset.
     *
     * @param mixed $key
     * @param mixed $value
     */
    public function offsetSet($key, $value)
    {
        $this->items->put($key, $value);
    }

    /**
     * Unset the item at the given key.
     *
     * @param mixed $key
     */
    public function offsetUnset($key)
    {
        $this->items->forget($key);
    }

    /**
     * Get the instance as an array.
     *
     * @return array
     */
    public function toArray()
    {
        return [
            'first' => $this->url(0),
            'last' => $this->lastPageUrl(),
            'next' => $this->nextPageUrl(),
            'prev' => $this->previousPageUrl(),
            'data' => $this->items->toArray(),
        ];
    }

    /**
     * Convert the object into something JSON serializable.
     *
     * @return array
     */
    public function jsonSerialize()
    {
        return $this->toArray();
    }

    /**
     * Convert the object to its JSON representation.
     *
     * @param int $options
     *
     * @return string
     */
    public function toJson($options = 0)
    {
        return json_encode($this->jsonSerialize(), $options);
    }
}

你需要这样称呼:

$options['queryParameter'] = [
    'page' => [
        'limit' => 10,
        'offset' => 0
    ],
    'path' => \Illuminate\Pagination\Paginator::resolveCurrentPath()
];
$result = new LengthAwareOffsetPaginator(
    collect($data),
    $totalItemsCount,
    $this->limit,
    $this->offset,
    $options
);

这将为您提供以下输出:

{
  "data": [
    {
        ....
    },
    {
        ....
    }
  ],
  "meta": {
    "pagination": {
      "total": 110,
      "count": 10,
      "per_page": 10,
      "current_page": 1,
      "total_pages": 11,
      "links": [
           "self": "url/pages?page=1",
           "next": "url/pages?page=2",
           "first": "url/pages?page=1",
           "last": "url/pages?page=11"
      ]
    }
  }
}

我想这会对你有所帮助。

【讨论】:

  • 你的意思是这样构造函数不需要实际的集合吗?你的意思是如果我在刀片中使用类似 $result->links 的东西,它会为我打印出导航布局?
  • @M.Shahrokhi 在此类中,您只需要传递单页记录集合。并且 $result->links 只会为您提供第一个、最后一个、下一个、自我和上一个 url .. 您可以在刀片中添加下一个/上一个链接.. 但是如果您想要所有分页链接,那么您必须进行这些自定义。
猜你喜欢
  • 2015-01-13
  • 2021-07-04
  • 2014-11-03
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
相关资源
最近更新 更多