php - 如何处理 laravel 资源中的嵌套关系
问题描述
所以我一直试图从我的“OrderShowResource”中获得第三级关系,但它不起作用,这是我的一些代码:
首先,我如何获取模型:
public function show($id)
{
return Order::with('carts.productPrice.product', 'pharmacies')
->isPerson()
->isNotDeleted()
->find($id);
}
$order = $this->orderServices->show($id);
throw_if(!$order, new \Exception('Not Found 404', 404));
return new OrderShowResource($order);
然后是“OrderShowResource”:
class OrderShowResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'tracking_code' => $this->tracking_code,
'status' => $this->statusName,
'carts' => OrderCartResource::collection($this->whenLoaded('carts')),
'pharmacies' => OrderPharmacyResource::collection($this->whenLoaded('pharmacies')),
'products' => ProductPricesResource::collection($this->whenLoaded('productPrice.product')),
'prescription_large' => isset($this->prescription) ? $this->prescriptionLarge : null,
'prescription_small' => isset($this->prescription) ? $this->prescriptionSmall : null,
'prescription_medium' => isset($this->prescription) ? $this->prescriptionMedium : null,
'price' => $this->price ?? null,
'total_price' => $this->total_price ?? null,
'address_id' => $this->address_id ?? null,
'address' => isset($this->address_id) ? optional($this->address)->name : null,
'delivery_price' => $this->delivery_price ?? null,
];
}
}
这是“ProductPricesResource”类:
class ProductPricesResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'product_id' => $this->product_id,
'brand_id' => $this->brand_id,
'price' => $this->price,
'brand'=>$this->brand,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
}
}
此外,所有关系都是就地的:购物车(订单模型)、产品价格(购物车模型)和产品(产品价格模型):
/**
* @return mixed
*/
public function carts()
{
return $this->belongsToMany(Cart::class);
}
/**
* @return mixed
*/
public function productPrice()
{
return $this->belongsTo(ProductPrice::class, 'product_price_id');
}
/**
* @return mixed
*/
public function product()
{
return $this->belongsTo(Product::class);
}
无论如何我可以通过 laravel 资源检索我的嵌套关系吗?
解决方案
您正在尝试包含具有两个嵌套模型的集合,我认为您不能使用本机 Resource 方法来做到这一点。我要做的是将所有需要的产品连接在一起的逻辑。我不是 100% 确定你的关系结构。
一个简单的Laravel
方法是使用收集方法。将每个购物车转换为新值的映射,在本例中为产品,这在关系方面是有意义的。
$products = $this->carts->map(function ($cart) {
return $cart->productPrice->product;
});
将它组合到资源中,就像这样。
'products' => ProductPricesResource::collection($products),
推荐阅读
- reactjs - Firebase 抛出错误 FirebaseApp 名称 [DEFAULT] 已存在
- mysql - MYSQL用另一个表中的名称替换id
- elasticsearch - logstash - 映射到带有转换的 json 数组
- here-api - 地理编码器停止返回形状
- search - 我正在使用支持 GPU 的 Milvus。但是为什么在我看来搜索有时比仅使用 CPU 的 Milvus 慢?
- python - Python OS - 使用“os.listdir”查看返回文件夹列表的目录。如何查看子文件夹的内容?
- scikit-learn - 无法在 colab 上运行 autosklearn
- c++ - TAPI 2.2 事件通知方式问题
- python - 在 Python 中定义类时,如何在 __init__() 中仅转换任何变量整数、列表等?
- html - 将图像放置在响应式页脚中