Only capture attributes when fetching user data#244
Conversation
| if ($user = $app->auth->user()) { | ||
| if (is_callable([$user, 'toArray'])) { | ||
| return $user->toArray(); | ||
| return $user->attributesToArray(); |
There was a problem hiding this comment.
We should check if attributesToArray is callable then, instead of toArray.
There was a problem hiding this comment.
True that. Easy one to miss even when testing it. 😅
cb5cac7 to
b885efe
Compare
| if ($user = $app->auth->user()) { | ||
| if (is_callable([$user, 'toArray'])) { | ||
| return $user->toArray(); | ||
| if (is_callable([$user, 'attributesToArray'])) { |
There was a problem hiding this comment.
This should actually be:
if (method_exists($user, 'attributesToArray') && is_callable([$user, 'attributesToArray'])) {
return $user->attributesToArray();
}This was a bug in the original code too.
The reason here is that eloquent models have a __call method, so is_callable returns true even for methods that aren't really callable, because it just throws a runtime exception if not callable. We can't just have a method_exists check here either, because that also returns true for private methods, and we are only interested in ones we can actually call.
There was a problem hiding this comment.
Hmm I see. Only models then? Would that be the case for the logger too? That's the only other place we're using is_callable.
There was a problem hiding this comment.
Technically yeh, it could be changed there. I don't think any of the out of the box loggers implement __call, but that check could be added there, yeh.
When a user is connected to many other models, calling toArray can be intensive, and the data gathered may not be much more useful from a debugging perspective. This change reduces the overhead of fetching the user.
b885efe to
4e74ecf
Compare
When a user is connected to many other models, calling
toArraycan be intensive, and the data gathered may not be much more useful from a debugging perspective. This change reduces the overhead of fetching the user.