为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Laravel 上填充 META 和 LINKS 分页?

如何在 Laravel 上填充 META 和 LINKS 分页?

PHP
侃侃尔雅 2021-09-18 16:01:01
我需要帮助在 laravel eloquent 上填充这种分页。{  "meta": {    "count": 10,    "total": 100  },  "links": {    "first": "http://localhost/page[limit]=10&page[offset]=0",    "last": "http://localhost/page[limit]=10&page[offset]=10",    "next": "http://localhost/page[limit]=10&page[offset]=10",    "prev": "null"  },  "data": [    {      "type": "checklists",      "id": "1"    }  ]}我在 Laravel Eloquent 上试过这段代码。$data = Model::select('type','id')->paginate(10);return response()->json(    [        'data' => $data    ],200);但它显示不同的格式,填充的数据没有 META 和 LINKS 模式。{    "data": {        "current_page": 1,        "data": [            {                "type": "Mechanical Equipment Sales Representative",                "id": 1            }       ],        "first_page_url": "http://localhost?page=1",        "from": 1,        "last_page": 4,        "last_page_url": "http://localhost?page=4",        "next_page_url": "http://localhost?page=2",        "path": "http://localhost",        "per_page": 10,        "prev_page_url": null,        "to": 10,        "total": 39    }}怎么做?请帮忙?
查看完整描述

1 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

您可以使用 API 资源:https : //laravel.com/docs/eloquent-resources#pagination


创建集合资源:


php artisan make:resource ModelCollection

在您的控制器中使用它:


$data = Model::select('type','id')->paginate(10);


return new ModelCollection($data);

对于 Lumen,创建一个app/Http/Resources/ModelCollection.php文件:


<?php


namespace App\Http\Resources;


use Illuminate\Http\Resources\Json\ResourceCollection;


class ModelCollection extends ResourceCollection

{

    /**

     * Transform the resource collection into an array.

     *

     * @param  \Illuminate\Http\Request  $request

     * @return array

     */

    public function toArray($request)

    {

        return parent::toArray($request);

    }

}


查看完整回答
反对 回复 2021-09-18
  • 1 回答
  • 0 关注
  • 123 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信