morphToMany(NirObject::class, 'objectable')->withTimestamps(); } public function getObjectAttribute() { return $this->objects()->first(); } public function getObject($typeName): ?Model { return ($type = ObjectType::query()->where(['name' => $typeName])->first()) ? $this->objects()->firstOrCreate(['type_id' => $type->id]) : null; } public function createObject($typeName): ?Model { return ($type = ObjectType::query()->where(['name' => $typeName])->first()) ? $this->objects()->create(['type_id' => $type->id]) : null; } public function getValue($fieldName) { return $this->object ? $this->object->getValue($fieldName) : null; } public function setValue($fieldName, $value) { return $this->object ? $this->object->setValue($fieldName, $value) : null; } public function setValues(array $values) { return $this->object ? $this->object->setValues($values) : null; } public function addValue($fieldName, $value) { return $this->object ? $this->object->addValue($fieldName, $value) : null; } }