Метод «with» внутри ресурса Collection Вызывается в другом ресурсе Collection, не возвращает - PullRequest
0 голосов
/ 28 января 2020

У меня есть отношения типа

У пользователя может быть много заказов ['user_id', 'id']. Заказать можно много заказов. OrderDetail hasOne Product

  • Пользователь ['id']
  • Order ['user_id', 'id']
  • OrderDetail ['order_id', 'product_id', 'количество'].

Теперь я использую ресурс коллекций для получения необходимых данных


    $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 в данном ответе.

Почему и как решить?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...