如何在Laravel资源中处理嵌套关系

时间:2019-03-28 10:10:21

标签: 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资源检索我的嵌套关系吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

像这样在资源中将其合并。

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