ContactType::class, 'phones' => 'array', ]; public function contactable(): MorphTo { return $this->morphTo(); } protected function fullName(): Attribute { return Attribute::get(function () { return trim("{$this->first_name} {$this->last_name}"); }); } protected function primaryPhone(): Attribute { return Attribute::get(function () { return $this->getPhoneByType('primary'); }); } protected function mobilePhone(): Attribute { return Attribute::get(function () { return $this->getPhoneByType('mobile'); }); } protected function faxPhone(): Attribute { return Attribute::get(function () { return $this->getPhoneByType('fax'); }); } protected function tollFreePhone(): Attribute { return Attribute::get(function () { return $this->getPhoneByType('toll_free'); }); } private function getPhoneByType(string $type): ?string { if (! is_array($this->phones)) { return null; } foreach ($this->phones as $phone) { if ($phone['type'] === $type) { return $phone['data']['number'] ?? null; } } return null; } protected static function newFactory(): Factory { return ContactFactory::new(); } }