Eloquent: Accessing non-existing property SHOULD throw an error #55188
Replies: 4 comments 9 replies
-
You can put this in your model for that protected function getAttributeFromArray($key)
{
return $this->getAttributes()[$key];
} We wanted to add this functionality in our free crud lib based on your idea but the above solution is simpler. UPDATE. The above will throw on a not hydrated model and also on appends. |
Beta Was this translation helpful? Give feedback.
-
On the whole I agree with the sentiment of the OP - the whole concept behind strong typing is to throw up errors when things are not done correctly - and providing a null as a value of a non-existent attribute is just asking for trouble later on. However, Eloquent cannot easily change to throw an exception without creating backwards compatibilities. Also, likely to apply to object types other than models. So I think that Laravel should handle this as it previously has and provide an ability to turn on strict attribute name checking through a call in the app ServiceProvider. |
Beta Was this translation helpful? Give feedback.
-
@Michal-Mikolas this is also the way PHP works: https://wiki.php.net/rfc/undefined_property_error_promotion, but that will change on PHP 9.0 |
Beta Was this translation helpful? Give feedback.
-
I wasn't aware of this change coming in PHP 9 however there is no planned release date for PHP v9 so it is some way in the future (with 8.5 at the end of this year). However, the current WARNING notices and future errors when attempting to access non-existent attributes will not apply to magic getters:
The real question is whether magic getters should do something similar or not. My gut reaction is that they should throw an exception for non-existent attributes however it does seem to me that one purpose (or possibly THE purpose) of implementing magic getters is to avoid this exact situation. What do others think? |
Beta Was this translation helpful? Give feedback.
-
Let's say we have model called
Article
with attributes (columns in DB):id
,title
,content
,is_secret
.In the Controller let's simply pass data from DB to the view:
and later in the template:
There is a typo (
is_secrel
instead ofis_secret
) which SHOULD throw an exception, warning or at least notice. I write "should" because this is how PHP behaves in every natural way and this is what normal PHP developers expect.But Eloquent is like "Oh, you want to access attribute which doesn't exist? Hmm, let's just return
null
, that could maybe work."The Problem
The only result of this behavior is that Eloquent helps to hide typos in the code. This could lead to bugs in the application and even security issues (e.g.
if (!$user->is_blocked) { let them log in }
).The Solution
Eloquent should (after checking that for the requested name there is no database column, accessor, relationship etc.) by default notice the developer they probably made a typo.
Beta Was this translation helpful? Give feedback.
All reactions