我需要帮助在 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);
}
}
- 1 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消