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
{
public function statuses()
{
return $this->hasMany(Status::class);
}
}
class Status extends Model
{
public function feature()
{
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.
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 thefeature_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.