-
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
updateOrCreate on belongsTo relationship not working as expected #29978
Comments
Also posted it here because I didn't know from which framework it comes: laravel/lumen-framework#967 |
Hmm not really sure what's going on here. Appreciating any help with figuring this out. |
A friend of mine said it's probably using _call to call the function because it doesn't exist on the belongsTo relation -> currenlty I worked arround it by setting the foreignkeys manually and then it seems to work, but I don't know why it isn't setting them itself |
I'm not sure if this is supposed to work. I think You can call |
But one would expect it to work on both sides of the relationship, don't they? |
IMO, |
Then maybe throw an error when used wrongly instead of having the create part work? |
@dvdbot might be a good update but sounds like that's more of a feature request. Feel free to open up an issue on the ideas repo. |
The whole beauty of Eloquent is in its relationships. |
+1 |
I got bit by this myself. The comments above about why it's not working (because the |
Description:
When trying to use the
updateOrCreate
method on a belongsTo relationship it's not working as expected: When there's no records, it creates one succesfully but it doesn't assign the id in the foreignkey field, when I know there's a record it just creates a new one because it can't match based on the id because the foreignkey isn't being set I also see the following in the query logs which will always return nothing because 0 is never equal to 1:Is this caused because the belongsTo doesn't have an implementation for the updateOrCreate as hasManyThrough in this issue: #16758
Steps To Reproduce:
ModelA
belongs toModelB
ModelA
and then try to updateOrCreate the related model like:The text was updated successfully, but these errors were encountered: