Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mutator can not handle array #2635

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

Closed
hans-thomas wants to merge 7 commits into mongodb:4.1 from hans-thomas:4.1
Closed

Mutator can not handle array #2635

hans-thomas wants to merge 7 commits into mongodb:4.1 from hans-thomas:4.1

Conversation

@hans-thomas
Copy link
Contributor

@hans-thomas hans-thomas commented Oct 7, 2023

Hi,
As mentioned in #2634, creating a mutator for the array type attribute will throw an error while setting the value to the attribute. This error will be thrown because of the invalid value prepared for the update action. So I override normalizeCastClassResponse method from Illuminate\Database\Eloquent\Concerns\HasAttributes trait and force it to add the key name all the time.

So if we have something like this on our model class

protected function skills(): Attribute
{
 return Attribute::make(
 get: fn ($value) => $value,
 set: fn ($values) => array_unique($values)
 );
}

we do not get an error anymore.

@hans-thomas hans-thomas marked this pull request as ready for review October 7, 2023 13:14
@GromNaN GromNaN self-requested a review October 7, 2023 18:48

This comment was marked as resolved.

Copy link
Contributor Author

@GromNaN I think this would be fix this issue. Now it's supporting mutators that return [ key => value ] to change the key name and also mutators that return an array of values. I write tests for both to prevent any breaking changes in the future.

Copy link
Contributor Author

So if the field's key can be an integer, this can't fix the problem and I think there is no way to solve this. Thank you for your time❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@GromNaN GromNaN GromNaN requested changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /