Alireza Amrollahi
Alireza Amrollahi

Reputation: 915

How to handle nested relation in laravel resource

So i've been trying to get a third level relation out of my "OrderShowResource" but it's not working , here is some of my code :

First, how i fetch the model :

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);

and then the "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,
        ];
    }
}

and here is the "ProductPricesResource" class :

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,
        ];
    }
}

Also all relations are in-place : carts (Order model) , productPrice (Cart model) and Product (ProductPrice model) :

 /**
 * @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);
}

Is there anyway i can retrieve my nested relation through laravel resource ?

Upvotes: 1

Views: 1809

Answers (1)

mrhn
mrhn

Reputation: 18926

You are trying to include A collection with two nested models, i do not think you can do this with natively Resource methods. What i would do is logic that concatenates all the needed products together. I'm not 100% sure of your relations structure.

An easy Laravel approach to this, is to use collection methods. Map that transforms each cart into a new value, in this case products, which makes sense in regards to the relations.

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

Combine it in the resource, like so.

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

Upvotes: 1

Related Questions