-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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.4][5.5] firstOrCreate(), updateOrCreate() excludes attributes on creation (when using $fillable) #21018
Comments
I think this the intended behavior when using If someone does something like this: $attendance->records()->updateOrCreate([
'st_id' => $record->st_id,
'rec_date' => $record->rec_date
], request()->all()); It could lead to a security problem if you bypass the In your case, you could try to unguard the model before inserting if you are sure that input is safe: AttendanceRecord::$unguarded = false;
$attendance->records()->updateOrCreate([
'st_id' => $record->st_id,
'rec_date' => $record->rec_date
], $record->getAttributes()); // also I think you want to pass the attributes as the values
AttendanceRecord::$unguarded = true; |
This would be a major breaking change if applied, and actually I don't think it should be applied at all, you should stop guarding all fields if you want this kind of flexibility. Closing since it's not a bug but feel free to start a discussion on laravel/internals. |
@themsaid I think it is logical bug... |
I think it would be good to be able to provide an argument to updateOrCreate to tell the underlying fill to use forceFill in cases where you are trying to get the system to create a record using data from a modified model instance. |
Description:
updateOrCreate() excludes key attributes on insertion.
When I look into the framework code deeply, I found that
updateOrCreate()
usesfirstOrNew()
function internally.The problem is
firstOrNew()
function usesnewModelInstance()
to get new Model instance. In this way, attribute which is not allowed by $fillable property would not be assigned to the model. (like st_id on my code below)framework/src/Illuminate/Database/Eloquent/Builder.php
Lines 391 to 396 in b353719
framework/src/Illuminate/Database/Eloquent/Builder.php
Lines 357 to 364 in b353719
Logically, search condition must be included in the record (especially on insertion).
So.. In my opinion,
firstOrCreate()
,firstOrNew()
,updateOrCreate()
shouldforceFill()
the given attributes.Steps To Reproduce:
When I run the code below
then I got SQL Error below
These are my Eloquent Model and Database
The text was updated successfully, but these errors were encountered: