Bug if BelongsToMany and model::shouldBeStrict #130
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Sorry for my English, I'll try to explain the bug.
Assuming that the relation is called
categories
, when doing a save, thecategories
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 sincecategories
is a relation so it is not configured infillable
of the model, so when doing afill
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 tofillable
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