1

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 ?

8
  • have you loaded the relationship? Commented Mar 28, 2019 at 11:05
  • @MartinHenriksen i did , i works other places as a relationship Commented Mar 28, 2019 at 11:30
  • can you post code on how you load it? Commented Mar 28, 2019 at 11:40
  • @MartinHenriksen i can , but can you explain what do you need to know about it? because as far as i know it's standard relation declaration. Commented Mar 28, 2019 at 12:06
  • 1
    You also need to post how you fetch the model and adds it to the resource, that is what i asked with "how it is loaded" Commented Mar 28, 2019 at 12:56

1 Answer 1

1

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),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.