重寫 API 資源分頁資料

莫須有發表於2020-06-22

laravel: 6.0+
5.8因資源內容不同,要根據不同情況調整

return $this->success('',new UploadCollection($upload));

控制器檔案trait

return response()->json([
        'code'=> 0,
        'message'=> $message,
        'data' => $data
],200);

直接使用

return new UploadCollection($upload);

Upload資源資料包裹中包含多餘資料

"links": {
        "first": "http://locahost/api/upload?page=1",
        "last": "http://locahost/api/upload?page=10",
        "prev": null,
        "next": "http://locahost/api/upload?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 10,
        "path": "http://locahost/api/upload",
        "per_page": 15,
        "to": 15,
        "total": 148
    }
namespace App\Http\Resources\Json;

use Illuminate\Http\Resources\Json\ResourceCollection;

class BaseResourceCollection extends ResourceCollection
{
    /**
     * Create a paginate-aware HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function preparePaginatedResponse($request)
    {
        if ($this->preserveAllQueryParameters) {
            $this->resource->appends($request->query());
        } elseif (! is_null($this->queryParameters)) {
            $this->resource->appends($this->queryParameters);
        }

        return (new CustomPaginatedResourceResponse($this))->toResponse($request);
    }

    /**
     * 返回應該和資源一起返回的其他資料陣列
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function with($request)
    {
        return [
            'code' => 0,
            'msg'  => '',
        ];
    }
}

CustomPaginatedResourceResponse 檔案

namespace App\Http\Resources\Json;

use Illuminate\Http\Resources\Json\PaginatedResourceResponse;

class CustomPaginatedResourceResponse extends PaginatedResourceResponse
{
    /**
     * Add the pagination information to the response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function paginationInformation($request)
    {
        $paginated = $this->resource->resource->toArray();

        return [
            // 'links' => $this->paginationLinks($paginated),
            'meta' => $this->meta($paginated),
        ];
    }

    protected function paginationLinks($paginated)
    {
        return [
            // 'prev' => $paginated['prev_page_url'] ?? null,
            // 'next' => $paginated['next_page_url'] ?? null,
        ];
    }

    protected function meta($paginated)
    {
        $metaData = parent::meta($paginated);
        return [
            'current_page' => $metaData['current_page'] ?? null,
            'last_page' => $metaData['last_page'] ?? null,
            'total' => $metaData['total'] ?? null,
        ];
    }
}

修改後返回值

    "meta": {
        "current_page": 1,
        "last_page": 10,
        "total": 148
    },
    "code": 0,
    "msg": ""

引入重寫BaseResourceCollection 即可正常使用,如果有更好的實現方式希望留下你的寶貴評論

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章