Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1138,4 +1138,15 @@ protected static function getMutatorMethods($class)

return $matches[1];
}

/**
* Checks if a specific attribute exists.
*
* @param string $key
* @return bool
*/
protected function hasAttribute($key)
{
return array_key_exists($key, $this->attributes);
Copy link
Contributor

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.

Copy link
Author

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?!

Copy link
Contributor

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.

Copy link
Author

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..

}
}