Skip to content

Commit

Permalink
[8.x] firstOrCreate and firstOrNew should merge attributes correc…
Browse files Browse the repository at this point in the history
…tly (#38346)

* firstOrCreate and firstOrNew should merge attributes correctly

* Fix tests

* Update HasOneOrMany
  • Loading branch information
JayBizzle authored Aug 13, 2021
1 parent 2d8836d commit 848a898
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
return $instance;
}

return $this->newModelInstance($attributes + $values);
return $this->newModelInstance(array_merge($attributes, $values));
}

/**
Expand All @@ -481,7 +481,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])
return $instance;
}

return tap($this->newModelInstance($attributes + $values), function ($instance) {
return tap($this->newModelInstance(array_merge($attributes, $values)), function ($instance) {
$instance->save();
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function findOrNew($id, $columns = ['*'])
public function firstOrNew(array $attributes = [], array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->related->newInstance($attributes + $values);
$instance = $this->related->newInstance(array_merge($attributes, $values));

$this->setForeignAttributesForCreate($instance);
}
Expand All @@ -232,7 +232,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
public function firstOrCreate(array $attributes = [], array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->create($attributes + $values);
$instance = $this->create(array_merge($attributes, $values));
}

return $instance;
Expand Down
47 changes: 47 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ public function testCursorPaginatedModelCollectionRetrievalWhenNoElementsAndDefa
$this->assertInstanceOf(CursorPaginator::class, $models);
}

public function testFirstOrNew()
{
$user1 = EloquentTestUser::firstOrNew(
['name' => 'Dries Vints'],
['name' => 'Nuno Maduro']
);

$this->assertSame('Nuno Maduro', $user1->name);
}

public function testFirstOrCreate()
{
$user1 = EloquentTestUser::firstOrCreate(['email' => '[email protected]']);
Expand All @@ -425,6 +435,13 @@ public function testFirstOrCreate()
$this->assertNotEquals($user3->id, $user1->id);
$this->assertSame('[email protected]', $user3->email);
$this->assertSame('Abigail Otwell', $user3->name);

$user4 = EloquentTestUser::firstOrCreate(
['name' => 'Dries Vints'],
['name' => 'Nuno Maduro', 'email' => '[email protected]']
);

$this->assertSame('Nuno Maduro', $user4->name);
}

public function testUpdateOrCreate()
Expand Down Expand Up @@ -678,6 +695,36 @@ public function testBasicModelHydration()
$this->assertCount(1, $models);
}

public function testFirstOrNewOnHasOneRelationShip()
{
$user1 = EloquentTestUser::create(['email' => '[email protected]']);
$post1 = $user1->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']);

$this->assertSame('New Post', $post1->name);

$user2 = EloquentTestUser::create(['email' => '[email protected]']);
$post = $user2->post()->create(['name' => 'First Post']);
$post2 = $user2->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']);

$this->assertSame('First Post', $post2->name);
$this->assertSame($post->id, $post2->id);
}

public function testFirstOrCreateOnHasOneRelationShip()
{
$user1 = EloquentTestUser::create(['email' => '[email protected]']);
$post1 = $user1->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']);

$this->assertSame('New Post', $post1->name);

$user2 = EloquentTestUser::create(['email' => '[email protected]']);
$post = $user2->post()->create(['name' => 'First Post']);
$post2 = $user2->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']);

$this->assertSame('First Post', $post2->name);
$this->assertSame($post->id, $post2->id);
}

public function testHasOnSelfReferencingBelongsToManyRelationship()
{
$user = EloquentTestUser::create(['email' => '[email protected]']);
Expand Down

0 comments on commit 848a898

Please sign in to comment.