自定义Laravel 5.5 Api资源集合分页 [英] Customising Laravel 5.5 Api Resource Collection pagination

查看:648
本文介绍了自定义Laravel 5.5 Api资源集合分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用laravel api资源.默认情况下,laravel提供链接和元,如下所示.

I have been working with laravel api resource. By default laravel provides links and meta as shown below.

"links": {
    "first": "https://demo.test/api/v2/taxes?page=1",
    "last": "https://demo.test/api/v2/taxes?page=4",
    "prev": null,
    "next": "https://demo.test/api/v2/taxes?page=2"
},
"meta": {
   "current_page": 1,
   "from": 1,
   "last_page": 4,
   "path": "https://demo.test/api/v2/taxes",
   "per_page": 2,
   "to": 2,
   "total": 8
} 

但是我不想要这个,我想要类似的东西

But I don't want this, insted i want something like

"pagination": {
  "total": 8,
  "count": 8,
  "per_page": 25,
  "current_page": 1,
  "total_pages": 1
}

我能够获得此信息,但是如果我执行return TaxResource::collection($taxes);,我将不会获得此信息.甚至我都有自定义的收集方法

I'm able to get this info but if I do return TaxResource::collection($taxes);, I won't get this. Even I have custom collection method

 public static function collection($resource)
    {
       $resource->pagination = [
        'total' => $resource->total(),
        'count' => $resource->count(),
        'per_page' => $resource->perPage(),
        'current_page' => $resource->currentPage(),
        'total_pages' => $resource->lastPage()
       ];
        return parent::collection($resource);
    }

它没有给我想要的东西.但是,如果我通过(TaxResource::collection($taxes))->pagination;引用,我就能做到.但我希望在执行return TaxResource::collection($taxes);

It is not giving what I want. But if I reference through (TaxResource::collection($taxes))->pagination; I'm able to get that. But I want it to be returned when I do return TaxResource::collection($taxes);

推荐答案

我对您的问题感兴趣,并花了一些时间来解决它.我想将来还有很多工作要做,以改进 Eloquent:API资源的功能.

I was interested in your question and spand some time to resolve it. I guess there are a lot of work to be done to improve Eloquent: API Resources' functionality in the future.

为了解决该问题,我必须使用资源集合而不是资源:

In order to resolve it I must use Resource Collections instead of Resources:

但是,如果您需要自定义集合返回的元数据,则必须定义资源集合

However, if you need to customize the meta data returned with the collection, it will be necessary to define a resource collection

php artisan make:资源税-征收

php artisan make:resource TaxCollection

路线:

Route::get('/get-taxes', function () {
    $taxes = Taxes::paginate();
    return new TaxCollection($s);
});

TaxCollection.php:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class TaxCollection extends ResourceCollection
{        
    public function toArray($request)
    {
        return [
        'data' => $this->collection,
        'pagination' => [
            'total' => $this->total(),
            'count' => $this->count(),
            'per_page' => $this->perPage(),
            'current_page' => $this->currentPage(),
            'total_pages' => $this->lastPage()
        ],
    ];
    }  

    // Using Laravel < 5.6
    public function withResponse($request, $response)
    {
        $originalContent = $response->getOriginalContent();
        unset($originalContent['links'],$originalContent['meta']);
        $response->setData($originalContent);
    }

    // Using Laravel >= 5.6
    public function withResponse($request, $response)
    {
        $jsonResponse = json_decode($response->getContent(), true);
        unset($jsonResponse['links'],$jsonResponse['meta']);
        $response->setContent(json_encode($jsonResponse));
    }
}

这解决了问题,但是现在有了新的问题: 与资源不同,我不知道如何修改资源集合中的toArray字段,该手册仅显示了带有'data' => $this->collection的示例,其中我们发送了未修改的集合(资源集合允许我们更改元数据.因此,如果仅使用资源,则可以修改集合数据,而不能修改元数据.

This solve the problem but now there are new one: Unlike Resources I don't know how to modify toArray fields in Resource Collections, the manual shows only example with 'data' => $this->collection where we send not modified collection (Resource Collections allows us change meta data). So If we use just Resource then we can modify collection data but not meta data.

这篇关于自定义Laravel 5.5 Api资源集合分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆