[英]Laravel format resource collection response error Property [product_id] does not exist on this collection instance
在 laravel 中,我们可以格式化来自资源类的 json 响应,如下所示
class ProductsResource extends JsonResource
{
public function toArray($request)
{
return [
'id'=> $this->product_id ,
'code'=> $this->product_code,
'shortdescription'=> $this->product_short_description,
'image'=> $this->product_image,
];
}
}
但是当返回资源集合时,我无法格式化我的集合错误属性 [product_id] 在这个集合实例上不存在
class ProductsResource extends ResourceCollection
{
public function toArray($request)
{
return [
'id'=> $this->product_id ,
'code'=> $this->product_code,
'shortdescription'=> $this->product_short_description,
'image'=> $this->product_image,
];
}
}
谢谢。
这是因为ResourceCollection
需要一collection
items 而不是single item
。 集合资源希望您遍历集合,并且不能直接从$this
执行单个实体例程(因为它是一个集合)。
您可能正在寻找的是投射自定义突变,可以在此处找到示例:
查找/搜索Value Object Casting
。 它彻底解释了如何在get
和set
上改变属性,这可能比资源集合更好(如果这是您唯一希望用它做的事情)。 这将立即修改集合,并使您不必在每次需要时手动实例化资源集合(因为您在模型级别进行修改)。
来自文档:
Value Object Casting
您不仅限于将值转换为原始类型。 您还可以将值转换为对象。 定义将值转换为对象的自定义转换与转换为原始类型非常相似; 然而,set 方法应该返回一个键/值对数组,用于在模型上设置原始的、可存储的值。
但是回到主题......
如果你倾倒而死: dd($this);
你会看到有一个名为+collection
的属性
如果您希望转换键或值,则必须遍历$this->collection
以将集合values
或keys
为您的要求。
正如您在父类Illuminate\\Http\\Resources\\Json\\ResourceCollection
看到的, toArray()
方法已经是一个映射集合。
您可以在其中看到它指向$this->collection
/**
* Transform the resource into a JSON array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection->map->toArray($request)->all();
}
您可以使用以下内容。 并更新此集合映射中的项目/键值。
return $this->collection->map(function($item, $key){})->toArray();
如果您希望在将值返回到数组之前对其进行转换。
或者像这样的简单 foreach(还没有测试过,并且有更好的方法来做到这一点)但是为了分享一个simple-to-grasp
例子:
$result = [];
// Map the associations to be modified
$resultMap = [
'product_id' => 'id',
'product_code' => 'code',
'product_short_description' => 'shortdescription',
'product_image' => 'image'
];
// Iterate through the collection
foreach ($this->collection as $index => $item)
foreach ($item as $key => $value)
$result[$index][$resultMap[$key]] = $value;
return $result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.