【问题标题】:Laravel resource collection - paginate - JSON response errorLaravel 资源集合 - 分页 - JSON 响应错误
【发布时间】:2019-08-22 08:08:12
【问题描述】:

我想在 Laravel API 上进行分页。结果,我想获取数据和状态码类型。

控制器:

public function index()
{
    $data = PersonCollection::collection(Person::paginate(2));

    return response()->json($data, 200);
}

PersonCollection 资源:

    public function toArray($request)
{
    return [
        'id' => $this->id,
        'first_name' => $this->first_name,
        'last_name' => $this->last_name,
        'email' => $this->email,
        'phone' => $this->phone,
        'city' => $this->city,
        'href' => [
            'link' => route('person.show', $this->id),
        ],
    ];
}

输出:

https://i.hizliresim.com/LvlBzj.png

[
{
"id": 1,
"first_name": "Burak Ali",
"last_name": "Ildır",
"email": "burak@gmail.com",
"phone": "376.395.7233",
"city": "Koelpinstad",
"href": {
"link": "http://127.0.0.1:8000/api/v2/person/1"
}
},
{
"id": 2,
"first_name": "Vena",
"last_name": "Spinka",
"email": "shields.carolyn@example.org",
"phone": "716-268-7788 x092",
"city": "South Gudrunbury",
"href": {
"link": "http://127.0.0.1:8000/api/v2/person/2"
}
}
]

但我想要。

https://i.hizliresim.com/LvlBWo.png

{
"data": [
{
"id": 1,
"first_name": "Burak Ali",
"last_name": "Ildır",
"email": "burak@gmail.com",
"phone": "376.395.7233",
"city": "Koelpinstad",
"href": {
"link": "http://127.0.0.1:8000/api/v2/person/1"
}
},
{
"id": 2,
"first_name": "Vena",
"last_name": "Spinka",
"email": "shields.carolyn@example.org",
"phone": "716-268-7788 x092",
"city": "South Gudrunbury",
"href": {
"link": "http://127.0.0.1:8000/api/v2/person/2"
}
}
],
"links": {
"first": "http://127.0.0.1:8000/api/v1/person?page=1",
"last": "http://127.0.0.1:8000/api/v1/person?page=26",
"prev": null,
"next": "http://127.0.0.1:8000/api/v1/person?page=2"
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 26,
"path": "http://127.0.0.1:8000/api/v1/person",
"per_page": 2,
"to": 2,
"total": 52
}
}

我想要其他页面链接。但是当我将其转换为 JSON 数据时,链接和元数据不会出现。

【问题讨论】:

  • 这正是您应该包含代码而不是代码图片链接的原因。你的链接现在是我的 403
  • 我刚加了。

标签: php json laravel laravel-5


【解决方案1】:

资源集合描述了模型集合如何以 JSON 格式传输。

您似乎将它用于单个模型。你需要的是:

public function toArray($request)
{
    return $this->collection->map(function ($person) {
        return [
            'id' => $person->id,
            'first_name' => $person->first_name,
            'last_name' => $person->last_name,
            'email' => $person->email,
            'phone' => $person->phone,
            'city' => $person->city,
            'href' => [
                 'link' => route('person.show', $person->id),
            ],
        ];
    });

}

但是推荐的方法是在同一个命名空间中创建一个PersonResource,并在该类中实现toArray($request):

人物

class Person extends Resource //The name is important
{
    public function toArray($request)
    {
       return [
            'id' => $this->id,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'email' => $this->email,
            'phone' => $this->phone,
            'city' => $this->city,
            'href' => [
                 'link' => route('person.show', $this->id),
            ],
        ];
    }
}

PersonCollection

class PersonCollection extends ResourceCollection
{ 
   // This class is intentionally empty
}

最后你应该让 Laravel 处理如何做出响应:

public function index()
{
    $data = PersonCollection::collection(Person::paginate(2));

    return $data->toResponse();
}

资源集合的默认行为是查找名称与集合类似但删除了Collection 部分的资源(在这种情况下,PersonCollection 将查找Person 资源)。

这应该确保每个模型都根据资源进行转换并保持分页行为。

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2022-07-26
    • 2020-06-25
    • 2021-01-29
    相关资源
    最近更新 更多