From 47809585e4cf4cc4abb0667337d64fc2936243a3 Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Thu, 10 Oct 2024 15:53:40 +0200 Subject: [PATCH 1/4] Fix retrieval of property to work for Astrotomic/translatable --- src/Normalizers/Normalized/NormalizedModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Normalizers/Normalized/NormalizedModel.php b/src/Normalizers/Normalized/NormalizedModel.php index 1aa3dfd7..1a935bcd 100644 --- a/src/Normalizers/Normalized/NormalizedModel.php +++ b/src/Normalizers/Normalized/NormalizedModel.php @@ -39,7 +39,7 @@ public function getProperty(string $name, DataProperty $dataProperty): mixed protected function fetchNewProperty(string $name, DataProperty $dataProperty): mixed { if ($this->hasModelAttribute($name)) { - return $this->properties[$name] = $this->model->getAttributeValue($name); + return $this->properties[$name] = $this->model->getAttribute($name); } $camelName = Str::camel($name); From b9b0c88e569e4fc60fbb24f5c3f94454a9d701ff Mon Sep 17 00:00:00 2001 From: Tofandel Date: Thu, 10 Oct 2024 16:26:47 +0200 Subject: [PATCH 2/4] Fix comment --- src/Normalizers/Normalized/NormalizedModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Normalizers/Normalized/NormalizedModel.php b/src/Normalizers/Normalized/NormalizedModel.php index 1a935bcd..a992e7f8 100644 --- a/src/Normalizers/Normalized/NormalizedModel.php +++ b/src/Normalizers/Normalized/NormalizedModel.php @@ -68,7 +68,7 @@ protected function hasModelAttribute(string $name): bool return $this->model->hasAttribute($name); } - // TODO: to use that one once we stop supporting Laravel 10 + // TODO: remove this once we stop supporting Laravel 10 if (! isset($this->attributesProperty)) { $this->attributesProperty = new ReflectionProperty($this->model, 'attributes'); From e7b5ce02e14cf081707db9166b800f904207e41f Mon Sep 17 00:00:00 2001 From: Tofandel Date: Sun, 20 Oct 2024 10:38:33 +0200 Subject: [PATCH 3/4] Load relations before attributes --- src/Normalizers/Normalized/NormalizedModel.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Normalizers/Normalized/NormalizedModel.php b/src/Normalizers/Normalized/NormalizedModel.php index a992e7f8..2443719f 100644 --- a/src/Normalizers/Normalized/NormalizedModel.php +++ b/src/Normalizers/Normalized/NormalizedModel.php @@ -2,6 +2,7 @@ namespace Spatie\LaravelData\Normalizers\Normalized; +use Illuminate\Database\Eloquent\MissingAttributeException; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; use ReflectionProperty; @@ -38,10 +39,6 @@ public function getProperty(string $name, DataProperty $dataProperty): mixed protected function fetchNewProperty(string $name, DataProperty $dataProperty): mixed { - if ($this->hasModelAttribute($name)) { - return $this->properties[$name] = $this->model->getAttribute($name); - } - $camelName = Str::camel($name); if ($dataProperty->attributes->contains(fn (object $attribute) => $attribute::class === LoadRelation::class)) { @@ -59,6 +56,14 @@ protected function fetchNewProperty(string $name, DataProperty $dataProperty): m return $this->properties[$name] = $this->model->getRelation($camelName); } + if (!$this->model->isRelation($name) && !$this->model->isRelation($camelName)) { + try { + return $this->properties[$name] = $this->model->getAttribute($name); + } catch (MissingAttributeException) { + // Fallback if missing Attribute + } + } + return $this->properties[$name] = UnknownProperty::create(); } @@ -69,15 +74,12 @@ protected function hasModelAttribute(string $name): bool } // TODO: remove this once we stop supporting Laravel 10 - if (! isset($this->attributesProperty)) { $this->attributesProperty = new ReflectionProperty($this->model, 'attributes'); - $this->attributesProperty->setAccessible(true); } if (! isset($this->castsProperty)) { $this->castsProperty = new ReflectionProperty($this->model, 'casts'); - $this->castsProperty->setAccessible(true); } return array_key_exists($name, $this->attributesProperty->getValue($this->model)) || From 138eeee3588f56362ce9023aef942fb762df536a Mon Sep 17 00:00:00 2001 From: Tofandel Date: Sun, 20 Oct 2024 08:39:04 +0000 Subject: [PATCH 4/4] Fix styling --- src/Normalizers/Normalized/NormalizedModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Normalizers/Normalized/NormalizedModel.php b/src/Normalizers/Normalized/NormalizedModel.php index 2443719f..66e975e7 100644 --- a/src/Normalizers/Normalized/NormalizedModel.php +++ b/src/Normalizers/Normalized/NormalizedModel.php @@ -56,7 +56,7 @@ protected function fetchNewProperty(string $name, DataProperty $dataProperty): m return $this->properties[$name] = $this->model->getRelation($camelName); } - if (!$this->model->isRelation($name) && !$this->model->isRelation($camelName)) { + if (! $this->model->isRelation($name) && ! $this->model->isRelation($camelName)) { try { return $this->properties[$name] = $this->model->getAttribute($name); } catch (MissingAttributeException) {