首页 > 解决方案 > 集合资源中的“with”方法在另一个集合资源中调用不会返回

问题描述

我有这样的关系

用户可以有许多订单 ['user_id','id']。Order可以有很多OrderDetail。订单详情有一个产品

现在我正在使用 Collection Resource 来获取所需的数据


    $myOrder = new MyOrderCollection($this->order); //return 


    // MyOrderCollection.php

    public function toArray($request)
    {
        return 
        [
            'data' => $this->collection->transform(function($order)
             {
                    return
                    [
                        'id'=> $order->id,
                        'orderDetails' => new  MyOrderDetailCollection($order->orderDetails),
                    ];
            })
        ];
    }

    public function with($request)
    {
        return 
        [
            'meta' => 
            [
                'amount' => $amount // return total of all the orders of that user
            ]
        ];
    }


    // MyOrderDetailCollection.php

    public function toArray($request)
        {
            return
                $this->collection->transform(function($singleOrder)
                {
                    return 
                     [
                        'id' => $singleOrder->id,

                        'quantity'  => $singleOrder->quantity,
                        'total' => $total, // (quantity * price) return total price of single item of order
                        'product_details' => new ProductResource($singleOrder->product)
                    ];
                });
        }

        public function with($request)
        {
            return
            [
                'meta'=> 
                 [
                    'grand_total_of_a_order' => $this->collection->sum('total')
                 ]
            ];
        }


      //result

      {
        "data": 
         [
            {
                "id": 2,
                "orderDetails":
                [
                    {
                        "id": 2,
                        "quantity": 7,
                        "total": 140,
                        "product_details": 
                        {
                           "price": 20
                        }
                    }
                ]
            },
            {
                "id": 1,
                "orderDetails": 
                 [
                    {
                        "id": 1,
                        "quantity": 2,
                        "total": 20,
                        "product_details": 
                        {
                            "price":10
                        }
                    },
                    {
                        "id": 1,
                        "quantity": 3,
                        "total": 90,
                        "product_details": 
                        {
                            "price":30
                        }
                    }
                ]
            }
        ],
        "meta": 
            {
            "amount":
                {
                    "total" = 250 //calculated from another file 
                }
            }
        }

 "meta": {
        "amount": {
            "grand_total_of_a_order" = xxx 
        }
    }

不返回从“with”方法调用。我认为应该在给定响应中的 order_details 之后返回。

为什么以及如何解决?

标签: laravellaravel-collection

解决方案


推荐阅读