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

Bug if BelongsToMany and model::shouldBeStrict #130

Merged
merged 1 commit into from
Nov 3, 2024

Conversation

gpibarra
Copy link
Contributor

@gpibarra gpibarra commented Nov 2, 2024

Sorry for my English, I'll try to explain the bug.

Assuming that the relation is called categories, when doing a save, the categories field remains in the form data, and then it would go to the model attributes as another field this field does not give an error because it does not reach the database since categories is a relation so it is not configured in fillable of the model, so when doing a fill of the form data in the model that field is not filled.

For example, on the Edit page (and the same thing happens on the Create page) https://github.com/filamentphp/panels/blob/3.x/src/Resources/Pages/EditRecord.php#L143 https://github.com/filamentphp/forms/blob/3.x/src/Concerns/HasState.php#L235

The above is not an error, it is the expected behavior.


However, using Model::shouldBeStrict() (widely recommended to avoid errors in the development stage, for example: https://planetscale.com/blog/laravels-safety-mechanisms) throws an exception when trying to fill a field that is not set to fillable in the model.

In these cases, the field must be dehydrated so that it is not attempted to be filled in the model, since the update of that field will not be via the model because it is not a field in the database, but it will be done with the sync of the relationship with the code of the saveRelationshipsUsing function.

This idea is copied from the original Select component https://github.com/filamentphp/forms/blob/3.x/src/Components/Select.php#L1066

@CodeWithDennis
Copy link
Owner

This seems to fix the problem some people have even without Model::shouldBeStrict(), thanks!

@CodeWithDennis CodeWithDennis merged commit 3b1bef4 into CodeWithDennis:3.x Nov 3, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants