我想使用 Resource 在流明上显示分页资源。
我需要如下所示的 json 结果:
{
"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": "items",
"id": "1"
}
]
}
这是我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Item\Eloquent\ItemModel as ItemModel;
use App\Http\Resources\ItemsResource;
public function completeitems(Request $request)
{
$res = ItemModel::paginate();
return new ItemsResource($res);
}
?>
这是我的ItemResource:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class ItemsResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
*
* @return array
*/
public function toArray($request)
{
return [
'type' => 'items',
'id' => $this->id
];
}
}
根据我的代码,结果是:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id
我已经尝试过很多次并用谷歌搜索错误但有点困惑。如何解决这个问题?谢谢你。