【问题标题】:How to get only specific fields from Laravel Eloquent API Resource?如何从 Laravel Eloquent API 资源中仅获取特定字段?
【发布时间】:2021-06-09 10:10:03
【问题描述】:

我正在开发一个可通过移动和网络应用程序访问的 API。
我需要从 API 资源发送特定字段。

我有ItemResource

public function toArray($request) {
    return [
        'id'          => $this->id,
        'name'        => $this->name,
        'description' => $this->description,
        'price'       => $this->price,
        'stock'       => $this->stock,
        'reviews'     => $this->reviews, // this is from reviews table
    ];
}

我的Item 模特

public function reviews() {
    return $this->hasMany(ItemReview::class);
}

我的控制器代码

return ItemResource::collection(Item::all());

我得到了这个数组

{
    "data": {
        "id": 10,
        "name": "Item Name",
        "description": "Item description",
        "price": 410,
        "stock": "815",
        "reviews": [
            {
                "id": 4,       // I don't want to get this field
                "item_id": 10, // I don't want to get this field
                "user_id": 9,  // I want to show user name instead of user id
                "review": "item review."
            },
            {
                "id": 12,      // I don't want to get this field
                "item_id": 10, // I don't want to get this field
                "user_id": 3,  // I want to show user name instead of user id
                "review": "item review."
            }
        ]
    }
}

我想得到一个这样的数组

{
    "data": {
        "id": 10,
        "name": "Item Name",
        "description": "Item description",
        "price": 410,
        "stock": "815",
        "reviews": [
            {
                "user_name": 'jhon',  
                "review": "item review."
            },
            {
                "user_name": 'jhon2',  
                "review": "item review."
            }
        ]
    }
}

我只想显示特定字段。
我该如何实现?

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    1。只有select你需要的东西

    如果您在reviews 之后添加括号,您可以返回一个Builder 实例,这将允许您向关系添加查询。
    有了这个,你可以select你需要的字段。

    public function toArray($request) {
        return [
            'id'          => $this->id,
            'name'        => $this->name,
            'description' => $this->description,
            'price'       => $this->price,
            'stock'       => $this->stock,
            'reviews'     => $this->reviews()->select('user_name', 'review')->get(),
        ];
    }
    

    2。创建一个ReviewResource

    您还可以为Review 创建一个resource,以返回它自己的数据。

    class ReviewResource extends JsonResource
    {
        public function toArray($request)
        {
            return [
                'user_name' => $this->user_name,
                'review' => $this->review,
            ];
        }
    }
    
    class ItemResource extends JsonResource
    {
        public function toArray($request) {
            return [
                'id'          => $this->id,
                'name'        => $this->name,
                'description' => $this->description,
                'price'       => $this->price,
                'stock'       => $this->stock,
                'reviews'     => ReviewResource::collection($this->reviews),
            ];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 2015-04-02
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      相关资源
      最近更新 更多