Replies: 1 comment
-
News? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The first argument of
updateOrCreate($attbributes, $values)
are the attributes it should look for in the database. If it can anything that matches these criteria, it will update that db entry with the values provided in the second argument. If it cannot find anything matching the attributes, it will create a new db entry (hence the name updateOrCreate).When you have a one-to-one relation (like
hasOne
ormorphOne
), the first argument kinda becomes obsolete IMO. The method should already have the information on which attributes (the foreign key of the relation + the type column if its a polymorphic relation) it should look for in the database based on the relation definition. Let me explain with an example:Example:
Reason I bring this up:
When you currently use
updateOrCreate
on ahasOne
/morphOne
relation with only the values provided (aka only 1 argument instead of the expected 2), it will always create a new entry in the database (except if you would explicitly have the foreign key in there, but you often don't mention that in the update statement).This becomes problematic if you are not aware of this behavior (like I was), since it will keep creating new entries instead of updating existing ones. Since this relation only returns 1 model (the one with the lowest ID), you will almost always keep getting the old (non-updated) model, while the db is being filled with new (unused) entries.
To conclude, my question:
Would it make sense to drop the first argument of
updateOrCreate()
on one-to-one relations or are there usecases where this is usefull/needed?Beta Was this translation helpful? Give feedback.
All reactions