-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.5] Add hasAttribute to Eloquent/Model #22250
Conversation
This PR adds the `hasAttribute()` method to check, if a given attribute exist in this model
*/ | ||
protected function hasAttribute($key) | ||
{ | ||
return array_key_exists($key, $this->attributes); |
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.
Will this work on mutated attributes? You should probably be using attributesToArray() instead of the attributes property.
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 am not sure on this.. because this method only checks if this key (i.e., the field) is set.. The value is not needed for this method..
As far as i know, mutators should only work on attributes
(e.g., apply additional functions on the values in order to change formatting or whatever). Therefore, $this->attributes
should be enough for the scope of this function..
Or am I missing something really critical?!
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.
Not quite true, although mutators should have a relationship with attributes someone could create a totally independent mutator.
But what I meant in the first place is, no mater if the attribute is a mutator or not this should return true, if not it could lead to confusion as I do not need to know if the attribute was a mutator in the first place or a real attribute.
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 changed it to attributesToArray()
- thank you for pointing that out..
Seems this is not consistent with other methods such as |
@tillkruss : i changed the name of the PR.. actually your rename-operation removed everything ;) @devcircus : Currently, there is no way of checking, if a given @jmarcher : i changed it to use |
Perfect, looks good to me |
Couldn't you fix the offsetExists method that is used inside __isset()? http://php.net/manual/en/language.oop5.overloading.php#object.isset And when you have that fixed you kinda could use hasAttribute as an alias method |
@DrowningElysium wouldn't that be way to much magic for this use-case here?! |
Well the PR seemed like provide an alternative for functions that are broken. So I thought it would be better to fix at least a bit of the issue and have the alternative for the property_exists function Seems like we are now building an alternative instead of fixing the issue we are having. |
the problem with "overloading" the Furthermore, i think, calling |
Now that you're checking |
@devcircus : currently, i have the use-case where i explicitly need to check, if a specific attribute exist in a model - and behave differently, if it does not.. So for certain use-cases it might be "important" to have such a helper.. |
any news on this PR here, @taylorotwell and community? |
|
Hey @taylorotwell and @GrahamCampbell .. My PR actually is exactly this line of code you suggested. However, always writing Then why even provide any new functions to laravel? Actually you can already use "built-in" functions from SPL in order to achieve whatever you want.. oh wait, i forgot.. SPL is quite ugly to use and "easy-to-use" helper functions are appreciated by developers.. So can you please re-open and merge this PR? Thank you very much! |
This won't work if In this case, And I'd like to see a |
I cannot believe this was rejected :( |
Totally ridiculous, shoud'nt be rejected |
It's added in Laravel 11.3: #50909 |
This PR adds the
hasAttribute()
method toEloquent/Model
in order to check, if a given attribute exist in this model.Why do we need this method?! Because other "checks", like
isset(...)
show a wrong behaviour.Consider the following example here:
Furthermore,
property_exists(...)
does not work either...So this would be a valuable contribution...