cuentas === null) { $this->cuentas = $this->parentOf(Cuenta::class, [Model::CHILD_KEY => 'categoria_id']); } return $this->cuentas; } protected $saldo; public function saldo() { if ($this->saldo === null) { $this->saldo = 0; if ($this->cuentas() !== null) { $this->saldo = array_reduce($this->cuentas(), function($sum, $item) { return $sum + $item->saldo(); }); } } return $this->saldo; } public function toArray(): array { $arr = parent::toArray(); $arr['saldo'] = $this->saldo(); $arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.'); return $arr; } }