*/ protected $fillable = [ 'declaration_id', 'type', 'body', 'sent_by_type', 'sent_by_id', 'metadata', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'type' => MessageType::class, 'sent_by_type' => ActorType::class, 'metadata' => 'array', ]; } /** * Get the declaration that owns the message. * * @return BelongsTo */ public function declaration(): BelongsTo { return $this->belongsTo(Declaration::class); } /** * Get the user who sent the message (when sent_by_type is user). * * @return BelongsTo */ public function senderUser(): BelongsTo { return $this->belongsTo(User::class, 'sent_by_id'); } /** * Get the client who sent the message (when sent_by_type is client). * * @return BelongsTo */ public function senderClient(): BelongsTo { return $this->belongsTo(Client::class, 'sent_by_id'); } /** * Get the sender display name. */ public function getSenderNameAttribute(): string { if ($this->sent_by_type?->is(ActorType::User)) { return $this->senderUser?->name ?? '—'; } if ($this->sent_by_type?->is(ActorType::Client)) { return $this->senderClient?->company_name ?? 'Client'; } return '—'; } }