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 documentation for Inserting & Updating Related Models includes multiple ways to insert related models, including save, saveMany, create, createMany, findOrNew, firstOrNew, firstOrCreate, and updateOrCreate. It then includes a link to the documentation about Upserts. However, upserts are not actually properly handled, as in this example:
class Feature extends Model
{
publicfunctionstatuses()
{
return$this->hasMany(Status::class);
}
}
class Status extends Model
{
publicfunctionfeature()
{
return$this->belongsTo(Feature::class);
}
}
When this code runs, it inserts all the rows into the statuses table with the feature_id set to null instead of being set to the correct ID value of the Feature.
I have a number of places where I want to upsert related models, and this pattern gets ugly fast.
I think I can do a PR to add this feature to upsert similar to how it is already done for updateOrCreate (using $this->getForeignKeyName() and $this->getParentKey()), but I wanted to get feedback before I start to see if that will be appreciated.
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
Uh oh!
There was an error while loading. Please reload this page.
-
The documentation for Inserting & Updating Related Models includes multiple ways to insert related models, including
save
,saveMany
,create
,createMany
,findOrNew
,firstOrNew
,firstOrCreate
, andupdateOrCreate
. It then includes a link to the documentation about Upserts. However, upserts are not actually properly handled, as in this example:When this code runs, it inserts all the rows into the statuses table with the
feature_id
set tonull
instead of being set to the correct ID value of theFeature
.Here is my current workaround:
I have a number of places where I want to upsert related models, and this pattern gets ugly fast.
I think I can do a PR to add this feature to
upsert
similar to how it is already done forupdateOrCreate
(using$this->getForeignKeyName()
and$this->getParentKey()
), but I wanted to get feedback before I start to see if that will be appreciated.(moved from laravel/ideas#2666)
Beta Was this translation helpful? Give feedback.
All reactions