Replies: 5 comments
-
more than 5% of execution time |
Beta Was this translation helpful? Give feedback.
-
Accessing an Eloquent model's attribute through the magic method ( In general, if you need to access an attribute quickly and don't need to run any accessors or mutators, it's recommended to use the getAttributeValue method. However, if you need to run accessors or mutators, you should use the magic method instead. |
Beta Was this translation helpful? Give feedback.
-
Given this disparity in performance (at least 25x difference) and the very common usage of the magic method, it does seem to me that the code to do the magic should be looked at to see if it could be significantly optimised (either always or for the majority of calls). |
Beta Was this translation helpful? Give feedback.
-
It was a false alarm, even if the line invoking the |
Beta Was this translation helpful? Give feedback.
-
If someone is finding this later on: To really speed up magic attributes, you'll have to add your own getter if the value is a primitive (string, integer), something like: class YourModel extends Model {
public function getId(): ?int
{
return $this->attributes['id'] ?? null;
}
} Getters like this are |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was profiling an API route and was surprised that accessing an Eloquent model magic attribute would take so long, then I tried using the

getAttributeValue
method directly and got significantly better performance, so much so that the profiler doesn't even consider it a hot path.Beta Was this translation helpful? Give feedback.
All reactions