generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix retrieval of property to work for Astrotomic/translatable #883
Open
Tofandel
wants to merge
4
commits into
spatie:main
Choose a base branch
from
Tofandel:patch-5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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->getAttributeValue($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(); | ||
} | ||
|
||
|
@@ -68,16 +73,13 @@ 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'); | ||
$this->attributesProperty->setAccessible(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line does nothing because: https://www.php.net/manual/en/reflectionproperty.setaccessible.php
And we require php 8.1 in composer.json |
||
} | ||
|
||
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)) || | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should get rid of this in the automatic camel, snake casing in the next major, after looking back at it, we should not do this manually for relations and rely on the content mapping of the lib https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/model-to-data-object#content-mapping-property-names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the commit for my proposal on how it should be handled in the next major 380942b
Attributes are still snake cased if necessary but not relations