【问题标题】:Laravel 8 - API, pagination and resource. "links" obj into "meta" keyLaravel 8 - API、分页和资源。 “链接” obj 到“元”键
【发布时间】:2021-01-10 15:00:07
【问题描述】:

我正在使用 Laravel 8 构建一个带有分页 JSON 的简单 API。

输出包含一个“链接”对象到“元”键:

{
  "data": [
    {
      . . .
    },
    {
      . . .
    }
  ],
  "links": {
    "prev": null,
    "first": "http://localhost/api/my-api?&page=1",
    "next": null,
    "last": "http://localhost/api/my-api?&page=2"
  },
  "meta": {
    "from": 1,
    "per_page": 400,
    "total": 477,
    "current_page": 1,
    "last_page": 2,
    "path": "http://localhost/api/my-api",
    "to": 400,
    "links": [
      {
        "url": null,
        "label": "Previous",
        "active": false
      },
      {
        "url": "http://localhost/api/my-api?&page=1",
        "label": 1,
        "active": true
      },
      {
        "url": "http://localhost/api/my-api?&page=2",
        "label": 2,
        "active": false
      },
      {
        "url": "http://localhost/api/my-api?&page=2",
        "label": "Next",
        "active": false
      }
    ]
  }
}

我没有找到任何关于它的文档;另外,官方文档也没有报告:

如何删除“链接”对象?

【问题讨论】:

    标签: api pagination laravel-8


    【解决方案1】:

    这有点诡计,因为我们不想接触源代码。在您的 Http/Controller/Api/V1 映射或任何您的 API 集合类存在的地方,创建一个名为 PaginateResourceResponseExtended.php 的文件。将此代码添加到文件中:

    namespace App\Http\Controllers\Api\V1;
    
    use Illuminate\Support\Arr;
    use Illuminate\Http\Resources\Json\PaginatedResourceResponse;
    
    class PaginateResourceResponseExtended extends PaginatedResourceResponse
    {
        /**
         * Gather the meta data for the response.
         *
         * @param  array  $paginated
         * @return array
         */
        protected function meta($paginated)
        {
            return Arr::except($paginated, [
                'data',
                'first_page_url',
                'last_page_url',
                'prev_page_url',
                'next_page_url',
                'links' //<----- THIS!
            ]);
        }
    }
    

    在您的资源集合类中,它是ResourceCollection 的扩展,添加以下内容: use App\Http\Controllers\Api\V1\PaginateResourceResponseExtended;

    在您的类中,您可以覆盖一个名为 preparePaginatedResponseResourceCollection 方法:

    public function preparePaginatedResponse($request)
    {
        if ($this->preserveAllQueryParameters) {
            $this->resource->appends($request->query());
        } elseif (! is_null($this->queryParameters)) {
            $this->resource->appends($this->queryParameters);
        }
    
        return (new PaginateResourceResponseExtended($this))->toResponse($request);
    }
    

    等等!

    当然,您也可以扩展Illuminate\Http\Resources\Json\ResourceCollection; 类,按照上述方法更改方法并将其扩展到您的所有资源集合。

    Illuminate\Http\Resources\Json\PaginatedResourceResponse 中受保护的 meta 方法是您想要更改的,只需将 'links' 添加到数组异常中即可。

    在下一个 Laravel 版本中,你覆盖的 Laravel 源文件方法不会改变?

    【讨论】:

      【解决方案2】:

      使用simplePaginate() 获得与文档中相同的示例。 https://laravel.com/docs/8.x/pagination#simple-pagination

      【讨论】:

      • 您的解决方案对我有用!
      猜你喜欢
      • 2019-04-25
      • 2021-12-08
      • 1970-01-01
      • 2020-05-17
      • 2020-03-30
      • 2019-09-09
      • 2022-01-23
      • 2022-01-07
      • 2016-09-28
      相关资源
      最近更新 更多