Building an array for a custom widget

Hello, trying for two days now to solve this.
//[Order]
- id
- payment_status
public function items()
{
  return $this->hasMany(OrderItem::class);
}
//[OrderItem]
- id
- order_id
- product_id
- quantity
- price
public function order()
{
  return $this->belongsTo(Order::class);
}
public function product()
{
  return $this->belongsTo(Product::class, 'product_id');
}
//[Product]
- id
- format_id
- theme_id 
public function items()
{
  return $this->hasMany(OrderItem::class);
}

   
 protected function getViewData(): array
    {
        $orders = Order::select('id')->where('payment_status', 1)
            ->with(['items' => function ($query) {
                $query->select('order_id', 'product_id');
                },
                'items.product' => function ($query) {
                    $query->select('format_id');
                }
            ])
            ->get()
            ->groupBy('format_id')
            ->toArray();

        return [
            'orders' => $orders
          ];
    }

What I need is to sum the quantity of items for orders with payment_status=1 and group them by format and build the widget array like this
[format][sum(quantity)]
Was this page helpful?