You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 or morphOne), 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:
// User classpublicfunctionphone(): hasOne
{
return$this->hasOne(Phone::class, 'user_id', 'id');
}
// How to use it updateOrCreate now:$user->phone()->updateOrCreate(
['user_id', $user->id], // For morphOne, you also need to provide the type here! This feels redundant, since already in relation
[
// Attributes on phone model
]
);
// How updateOrCreate should (in my opinion) work for relations with only 1 model$user->phone()->updateOrCreate([
// Attributes on phone model
]);
Reason I bring this up:
When you currently use updateOrCreate on a hasOne/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?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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