belongsTo(User::class); } public function orders(): HasMany { return $this->hasMany(AffiliateOrder::class); } /** * Get the earnings made by this affiliate * * @return string */ public function earnings(): Attribute { return Attribute::make( get: function (): array { $earnings = []; $this->orders->each(function ($order) use (&$earnings) { foreach ($order->earnings as $currency => $total) { if (!isset($earnings[$currency])) { $earnings[$currency] = 0; } $earnings[$currency] += $total; } }); return $earnings; }, ); } }