id); // Eager load relationships $this->load(['accountFrom.accountable', 'accountTo.accountable']); // Extract relationship data safely $accountFrom = $this->accountFrom; $accountTo = $this->accountTo; $accountFromName = $accountFrom->name ?? ''; $accountToName = $accountTo->name ?? ''; $relationFromName = $accountFrom && $accountFrom->accountable ? $accountFrom->accountable->first()->name ?? '' : ''; $relationToName = $accountTo && $accountTo->accountable ? $accountTo->accountable->first()->name ?? '' : ''; // Log the loaded relationships for debugging // info('Account From:', ['accountFrom' => $accountFrom]); // info('Account To:', ['accountTo' => $accountTo]); return [ 'id' => $this->id, 'created_at' => $this->created_at, 'from_account_id' => $this->accountFrom, 'to_account_id' => $this->accountTo, 'amount' => $this->amount, 'account_from_name' => $accountFromName, 'account_to_name' => $accountToName, 'relation_from' => 'From ' . $relationFromName, 'relation_to' => 'To ' . $relationToName, 'description' => $this->description ?? '', ]; } public function accountFrom() { return $this->belongsTo(Account::class, 'from_account_id'); } public function accountTo() { return $this->belongsTo(Account::class, 'to_account_id'); } public function creator() { return $this->belongsTo(User::class, 'creator_user_id'); } /** * Get the transaction type that owns the transaction. */ public function transactionType() { return $this->belongsTo(TransactionType::class); } }