Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] firstOrCreate and firstOrNew should merge attributes correctly #38346

Merged
merged 3 commits into from
Aug 13, 2021
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
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