From df2e8421c4df705f48c2caafe4c0d6d510bcd544 Mon Sep 17 00:00:00 2001 From: Alexandr Chernyaev Date: Sat, 9 Nov 2024 01:39:53 +0300 Subject: [PATCH] Fast Attribute Access (#2921) Improve performance for simple model attribute --- src/Screen/AsSource.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Screen/AsSource.php b/src/Screen/AsSource.php index a2a1e2a278..1f3fcd1970 100644 --- a/src/Screen/AsSource.php +++ b/src/Screen/AsSource.php @@ -22,10 +22,16 @@ trait AsSource * * @return mixed|null The value of the field, or null if the field is not found. */ - public function getContent(string $field) + public function getContent(string $field): mixed { - return Arr::get($this->toArray(), $field) // Try to get the field value from the object's array representation. + // When field does not contain a dot, it is a simple field name. + // And we not need to use cast model to array for this case. + if (! str_contains($field, '.')) { + return $this->getAttribute($field); + } + + return $this->getAttribute($field) // Try to get the field value from the object's attributes. ?? Arr::get($this->getRelations(), $field) // Try to get the field value from the object's relations. - ?? $this->getAttribute($field); // Try to get the field value from the object's attributes. + ?? Arr::get($this, $field); // Try to get the field value from the object's array representation. } }