首页 > 解决方案 > 如何处理 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 资源检索我的嵌套关系吗?

标签: phplaravel

解决方案


您正在尝试包含具有两个嵌套模型的集合,我认为您不能使用本机 Resource 方法来做到这一点。我要做的是将所有需要的产品连接在一起的逻辑。我不是 100% 确定你的关系结构。

一个简单的Laravel方法是使用收集方法。将每个购物车转换为新值的映射,在本例中为产品,这在关系方面是有意义的。

$products = $this->carts->map(function ($cart) {
    return $cart->productPrice->product;
});

将它组合到资源中,就像这样。

'products' => ProductPricesResource::collection($products),

推荐阅读