php Laravel 5.5使用数组的API资源收集

de90aj5v  于 2023-02-07  发布在  PHP
关注(0)|答案(2)|浏览(135)

我正在使用laravel 5.5 API资源集合来显示产品列表。我正在使用数组而不是模态对象。我得到了项目列表,但我不确定如何在响应中显示项目变量数组。传递到API资源的实际响应数组为:

{
"ID": 3160,
"UserID": 3302,
"ItemName": "2",
"Description": "2",
"Price": 2,
"StockLimited": true,
"StockQuantity": 19,
"CurrencyCode": "USD",
"ImageUrl": "/images/items/Item3302-636434761494584365-qq5HFm.png",
"VariantItems": [
  {
    "ID": 3181,
    "ItemName": "2",
    "Price": 0,
    "StockLimited": false,
    "StockQuantity": 0,
    "CurrencyCode": "USD",
    "Variants": [
      {
        "Id": 1099,
        "Name": "Red",
        "VariantGroupId": 1027,
        "VariantGroupName": "Colour"
      },
      {
        "Id": 1111,
        "Name": "S",
        "VariantGroupId": 1028,
        "VariantGroupName": "Size"
      }
    ]
  },
  {
    "ID": 3182,
    "ItemName": "2",
    "Price": 0,
    "StockLimited": false,
    "StockQuantity": 0,
    "CurrencyCode": "USD",
    "Variants": [
      {
        "Id": 1099,
        "Name": "Red",
        "VariantGroupId": 1027,
        "VariantGroupName": "Colour"
      },
      {
        "Id": 1112,
        "Name": "M",
        "VariantGroupId": 1028,
        "VariantGroupName": "Size"
      }
    ]
  }

],
"MerchantName": "seller",
"VariantGroupLists": [
  {
    "VariantGroupName": "Colour",
    "Names": [
      "Red",
      "Grey",
      "Navy"
    ]
  },
  {
    "VariantGroupName": "Size",
    "Names": [
      "S",
      "M",
      "L",
      "XL"
    ]
  }
]  
}

我的项目资源集合

public function toArray($request)
{

    return [
        'data' => $this->collection->map(function ($item) use ($request) {
            return (new ItemResource($item))->toArray($request);
        })
    ];
}

我的项目资源

public function toArray($request)
{

    return [
        "item_id" => $this->resource->ID,
        "item_name" => $this->resource->ItemName,
        "description" =>$this->resource->Description,
        "item_price"=>$this->resource->Price,
        "stock_qty"=>$this->resource->StockQuantity,
        "currency_code"=>$this->resource->CurrencyCode,
        "item_image_url"=>$this->resource->ImageUrl,

    ];

}

public function with($request)
{

    return ['item_varients' => [new ItemResource($this->resource->VariantItems)]]; 
}

我试图达到的是这样的结果:

[

{
    "item_id": 3160,
    "item_name": "BLACK COWL NECK DRESS WITH DIAMANTE DETAIL",
    "description": "Test test",
    "item_price": 16.99,
    "stock_qty": 20,
    "currency_code": "SGD",
    "item_image_url": "/images/items/Item18-636231488325192562-GoiIBl.png",
    "item_varients": [
      {
        "id": 29,
        "name": "Red",
        "varientgroupId": 11,
        "varientgroupname": "Color"
      }
    ]
  }
]

ItemResource中的with()方法不起作用。如何在资源响应中添加"item_varients"数组?

tzdcorbm

tzdcorbm1#

我将首先移动该行,在toArray方法中添加item_varients属性,如下所示:

public function toArray($request)
{
    return [
        "item_id" => $this->resource->ID,
        "item_name" => $this->resource->ItemName,
        "description" => $this->resource->Description,
        "item_price" => $this->resource->Price,
        "stock_qty" => $this->resource->StockQuantity,
        "currency_code" => $this->resource->CurrencyCode,
        "item_image_url" => $this->resource->ImageUrl,
        "item_varients" => [
            new ItemResource($this->resource->VariantItems)
        ]
    ]
}

meta信息

我们这样做是因为with是用来添加一个 meta信息块或类似的东西的,请参见https://laravel.com/docs/5.5/eloquent-resources#adding-meta-data使用with方法的优点是,多个资源只会导致一个meta块,我认为这不是你想要的。

VariantItem的资源

我认为你必须为VariantItem构建一个Resource类。因为VariantItem是一个集合,所以你也必须填充名为item_variants的数组,类似于你的ItemResourceCollection。所以把你的代码修改成这样:

public function toArray($request)
{
    /**
     * Prepare your variant items to have a collection
     *
     * @var Collection $variantItems
     */
    $variantItems = collect($this->resource->VariantItems);

    return [
        "item_id" => $this->resource->ID,
        "item_name" => $this->resource->ItemName,
        "description" => $this->resource->Description,
        "item_price" => $this->resource->Price,
        "stock_qty" > $this->resource->StockQuantity,
        "currency_code" => $this->resource->CurrencyCode,
        "item_image_url" => $this->resource->ImageUrl,
        "item_varients" => VariantItem::collection($variantItems)
    ]
}
c7rzv4ha

c7rzv4ha2#

以下是使用集合获取特定字段的最佳方法

return [
        'id' => $this->id,
        'variant_item' => new CustomResource($this->user, ['item_id', 'item_name', 'item_size'])
    ];

相关问题