-
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
[9.x] Add Eloquent mode to prevent silently discarding fills for attributes not in $fillable
#43893
Merged
taylorotwell
merged 6 commits into
laravel:9.x
from
ralphjsmit:throw-fillable-exceptions-also-locally
Sep 1, 2022
Merged
[9.x] Add Eloquent mode to prevent silently discarding fills for attributes not in $fillable
#43893
taylorotwell
merged 6 commits into
laravel:9.x
from
ralphjsmit:throw-fillable-exceptions-also-locally
Sep 1, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ralphjsmit
changed the title
[9.x] Always throw MassAssignmentExceptions locally
[9.x] Add method to prevent discarding changes to guarded attributes and throw an exception instead
Aug 27, 2022
ralphjsmit
changed the title
[9.x] Add method to prevent discarding changes to guarded attributes and throw an exception instead
[9.x] Add Eloquent mode to prevent prevently silently discarding fills for attributes not in Aug 27, 2022
$fillable
add tests to your codes, thank you. |
Love this one, thanks @ralphjsmit! |
This should be the the Laravel default. |
ralphjsmit
changed the title
[9.x] Add Eloquent mode to prevent prevently silently discarding fills for attributes not in
[9.x] Add Eloquent mode to prevent silently discarding fills for attributes not in Sep 7, 2022
$fillable
$fillable
@tillkruss Agree! You could PR it though, perhaps Taylor is open to making it the default in 9.x or 10.x. 👊 |
@ralphjsmit this PR actually doesn't work for simple cases like:
|
Attempted fix here: eff2275 |
This was referenced Sep 26, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
One very irritating thing for me is to forget adding a new property to the
$fillable
array (because it's not my default way of working) and later discover that fills have been silently discarded, thus causing bugs.Personally I always unguard my models using
$guarded = []
, because I know from myself that I never do anything like$model->update($request->all())
. However, sometimes you join a new project where it's the convention to always use the$fillable
array.Currently Eloquent only throws an exception when the model is "totally guarded". This PR adds a method that is similar to the
Model::preventLazyLoading()
to also throw exceptions when a model change was discarded:Model::preventDiscardingGuardedAttributeFills();
It also works with a boolean:
Naming ideas are appreciated. I tried to come up with a shorter name, but I couldn't yet think of a name that could convey the behaviour in a better and shorter way.
People can also add a callback to handle the exception. The callback receives the model, the key and the value of the attribute:
I think this would be very useful to prevent bugs on local environments when the developer forgets to add an attribute to
$fillable
. Thanks!