我正在做一个电子商务网站,在那里人们可以对产品提供报价。我想在我得到产品的地方显示产品的所有报价。之后,我想在同一个查询中获得关于提供报价的用户和父报价的信息。
我是这样得到产品的
`public function show(Product $product)
{
// return response()->json($product);
return new SingleProductResource($product);
}`
SingleProductResource返回以下内容
`public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'type' => $this->type,
'images' => $this->images,
'status' => $this->status,
'owner' => $this->user,
'offers' => $this->offers,
];
}`
Offers返回一个这样的数组
`"offers": [
{
"id": 2,
"user_id": null,
"product_id": 1,
"offer_amount": "3",
"parent_id": 2,
"created_at": "2022-11-12T07:54:10.000000Z",
"updated_at": "2022-11-12T07:54:10.000000Z"
},
{
"id": 4,
"user_id": 1,
"product_id": 1,
"offer_amount": "3",
"parent_id": 2,
"created_at": "2022-11-12T08:01:29.000000Z",
"updated_at": "2022-11-12T08:01:29.000000Z"
},
{
"id": 5,
"user_id": null,
"product_id": 1,
"offer_amount": "3",
"parent_id": null,
"created_at": "2022-11-12T08:01:56.000000Z",
"updated_at": "2022-11-12T08:01:56.000000Z"
}
]`
但是我想在这个查询中直接得到用户信息,我不能在资源中这样做(见下面的注解),因为$this-〉offers返回一个数组。
`return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'type' => $this->type,
'images' => $this->images,
'status' => $this->status,
'creator' => $this->user,
'offers' => $this->offers->user, //this
];`
1条答案
按热度按时间q3qa4bjr1#
您需要以某种方式修剪您的$product。例如