Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ public function updateOrCreate(array $attributes, array $values = [])
});
}

/**
* Insert new records or update the existing ones.
*
* @param array $values
* @param array|string $uniqueBy
* @param array|null $update
* @return int
*/
public function upsert(array $values, $uniqueBy, $update = null)
{
foreach ($values as $key => $value) {
$values[$key][$this->getForeignKeyName()] = $this->getParentKey();
}

return $this->getQuery()->upsert($values, $uniqueBy, $update);
}

/**
* Attach a model instance to the parent model.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ protected function setForeignAttributesForCreate(Model $model)
$model->{$this->getMorphType()} = $this->morphClass;
}

/**
* Insert new records or update the existing ones.
*
* @param array $values
* @param array|string $uniqueBy
* @param array|null $update
* @return int
*/
public function upsert(array $values, $uniqueBy, $update = null)
{
foreach ($values as $key => $value) {
$values[$key][$this->getMorphType()] = $this->getMorphClass();
}

return parent::upsert($values, $uniqueBy, $update);
}

/** @inheritDoc */
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ public function testUpdateOrCreateMethodCreatesNewModelWithForeignKeySet()
$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
}

public function testRelationUpsertFillsForeignKey()
{
$relation = $this->getRelation();

$relation->getQuery()->shouldReceive('upsert')->with(
[
['email' => 'foo3', 'name' => 'bar', $relation->getForeignKeyName() => $relation->getParentKey()],
['name' => 'bar2', 'email' => 'foo2', $relation->getForeignKeyName() => $relation->getParentKey()],
],
['email'],
['name']
);

$relation->upsert(
[
['email' => 'foo3', 'name' => 'bar'],
['name' => 'bar2', 'email' => 'foo2'],
],
['email'],
['name']
);
}

public function testRelationIsProperlyInitialized()
{
$relation = $this->getRelation();
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ public function testMorphManyEagerConstraintsAreProperlyAdded()
$relation->addEagerConstraints([$model1, $model2]);
}

public function testMorphRelationUpsertFillsForeignKey()
{
$relation = $this->getManyRelation();

$relation->getQuery()->shouldReceive('upsert')->with(
[
['email' => 'foo3', 'name' => 'bar', $relation->getForeignKeyName() => $relation->getParentKey(), $relation->getMorphType() => $relation->getMorphClass()],
['name' => 'bar2', 'email' => 'foo2', $relation->getForeignKeyName() => $relation->getParentKey(), $relation->getMorphType() => $relation->getMorphClass()],
],
['email'],
['name']
);

$relation->upsert(
[
['email' => 'foo3', 'name' => 'bar'],
['name' => 'bar2', 'email' => 'foo2'],
],
['email'],
['name']
);
}

public function testMakeFunctionOnMorph()
{
$_SERVER['__eloquent.saved'] = false;
Expand Down