Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ethar's avatar
Level 5

return array as value in api Resource

i have this data come from eloquent

[
    {
        "id": 44,
        "payament_method": 1,
        "recivedType": 1,
        "reciveLocation": 1,
        "status": 0,
        "created_at": "2021-06-14T17:23:20.000000Z",
        "user_cart_items": [
            {
                "id": 3,
                "price": 2.5,
                "qty": 2,
                "product_id": 2,
                "user_cart_id": 44,
                "product": {
                    "id": 2,
                    "name_ar": "Blaze Holcomb"
                }
            },
            {
                "id": 4,
                "price": 10,
                "qty": 2,
                "product_id": 1,
                "user_cart_id": 44,
                "product": {
                    "id": 1,
                    "name_ar": "Marah Cole"
                }
            }
        ]
    }
]

but when use Api Resource

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'paymentMethod' => $this->payament_method,
            'receivedType' => $this->recivedType,
            'receivedLocation' => $this->reciveLocation,
            'created_at' => $this->created_at,
            'status' => $this->status,
            'user_cart_items' => [
                'product_name'=>$this->user_cart_items->product->name_ar,
                'price'=>$this->user_cart_items->price,
                'qty'=>$this->user_cart_items->qty,
            ],
        ];
    }

eloquent

    public function myOrders($user_id){
        return UserOrderResource::collection(
UserCart::select('id','payament_method','recivedType','reciveLocation','status','created_at')
            ->with((['user_cart_items' => function ($q) {
                $q->select('id', 'price', 'qty', 'product_id','user_cart_id')
                    ->with('product:id,name_ar');
            }]))
            ->where('user_id',$user_id)
            ->get());
    }

i got this error

Exception: Property [product] does not exist on this collection instance.
0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

OPTION 1

Your nested resources (user_cart_items) can be a Resource Collection itself.

// UserOrderResource
public function toArray($request)
{
    return [
        'id' => $this->id,
        'paymentMethod' => $this->payament_method,
        'receivedType' => $this->recivedType,
        'receivedLocation' => $this->reciveLocation,
        'created_at' => $this->created_at,
        'status' => $this->status,
        'user_cart_items' => UserCartItemResource::collection($this->whenLoaded('user_cart_items')),
    ];
}
// UserCartItemResource
public function toArray($request)
{
    return [
        'product_name'=>$this->product->name_ar,
        'price'=>$this->price,
        'qty'=>$this->qty,
    ];
}

OPTION 2

You would need to map over the Collection of user_cart_items and build up the array representation:

// UserOrderResource
public function toArray($request)
{
    return [
        'id' => $this->id,
        'paymentMethod' => $this->payament_method,
        'receivedType' => $this->recivedType,
        'receivedLocation' => $this->reciveLocation,
        'created_at' => $this->created_at,
        'status' => $this->status,
        'user_cart_items' => $this->user_cart_items->map(function ($item) {
			return [
                'product_name'=>$item->product->name,
                'price'=>$item->price,
                'qty'=>$item->qty,
            ];
		})->all()
    ];
}
1 like

Please or to participate in this conversation.