hasMany(BankAccount::class, 'institution_id'); } public function connectedBankAccounts(): HasMany { return $this->hasMany(ConnectedBankAccount::class, 'institution_id'); } public function getLastTransactionDate(): ?string { $latestDate = $this->connectedBankAccounts->map(function ($connectedBankAccount) { if ($connectedBankAccount->bankAccount) { return $connectedBankAccount->bankAccount->transactions()->max('posted_at'); } return null; })->filter()->max(); if ($latestDate) { return Carbon::parse($latestDate)->diffForHumans(); } return null; } protected function logoUrl(): Attribute { return Attribute::get(static function (mixed $value, array $attributes): ?string { if ($attributes['logo']) { return Storage::disk('public')->url($attributes['logo']); } return null; }); } public function logo(): Attribute { return Attribute::set(static function (mixed $value): ?string { if ($value) { $decoded = base64_decode($value); $filename = 'institution_logo_' . uniqid('', true) . '.png'; Storage::disk('public')->put($filename, $decoded); return $filename; } return null; }); } }