[5.x] Make custom attribute value available for attribute casts#1127
[5.x] Make custom attribute value available for attribute casts#1127
Conversation
| protected function getAttributeFromArray($key) | ||
| { | ||
| return parent::getAttributeFromArray($key) ?? $this->getCustomAttribute($key); | ||
| } |
There was a problem hiding this comment.
Is this to patch the internal function for $value to get the correct value?
What about calling $this->getAttributeFromArray('price') ?? $this->getCustomAttribute('price') manually?
There was a problem hiding this comment.
Yes, you definitely could do that manually each time. It would turn the functions for images from
protected function image(): Attribute
{
return Attribute::get($this->getImageFrom(...));
}into something like:
protected function image(): Attribute
{
return Attribute::get(function() {
$value = $this->getCustomAttribute('image')?->value;
$this->getImageFrom($value);
});
}I'm definitely not a fan of that.
You can see how I've also removed the need for this from the price attribute in this PR. Personally I don't think it should be necessary to call such an internal function every time, especially because there's no guarantee that it'll stay the same in the future.
|
This doesn't have any impact on performance? |
|
@Jade-GG + @indykoning 😇 |
|
I've tried a few different benchmarks and I was unable to find any statistically significant difference in performance. I wasn't even sure if my back-and-forth branch switching was actually doing something at first, because everything looked the exact same and had the same numbers. |
|
Follow up: RAP-1813 |
I ran into the issue that images did not work anymore. This ended up being because the
$valuegiven to attribute casts was always null.By hooking into the
getAttributeFromArrayfunction we can send the right value through. Alternatively, we could hook intogetAttributes()and merge everything into there, however that would add a significant performance overhead.