8000 [8.x] `firstOrCreate` and `firstOrNew` should merge attributes correc… · laravel/framework@848a898 · GitHub
[go: up one dir, main page]

Skip to content

Commit 848a898

Browse files
authored
[8.x] firstOrCreate and firstOrNew should merge attributes correctly (#38346)
* firstOrCreate and firstOrNew should merge attributes correctly * Fix tests * Update HasOneOrMany
1 parent 2d8836d commit 848a898

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
465465
return $instance;
466466
}
467467

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

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

484-
return tap($this->newModelInstance($attributes + $values), function ($instance) {
484+
return tap($this->newModelInstance( 10000 array_merge($attributes, $values)), function ($instance) {
485485
$instance->save();
486486
});
487487
}

src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public function findOrNew($id, $columns = ['*'])
214214
public function firstOrNew(array $attributes = [], array $values = [])
215215
{
216216
if (is_null($instance = $this->where($attributes)->first())) {
217-
$instance = $this->related->newInstance($attributes + $values);
217+
$instance = $this->related->newInstance(array_merge($attributes, $values));
218218

219219
$this->setForeignAttributesForCreate($instance);
220220
}
@@ -232,7 +232,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
232232
public function firstOrCreate(array $attributes = [], array $values = [])
233233
{
234234
if (is_null($instance = $this->where($attributes)->first())) {
235-
$instance = $this->create($attributes + $values);
235+
$instance = $this->create(array_merge($attributes, $values));
236236
}
237237

238238
return $instance;

tests/Database/DatabaseEloquentIntegrationTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,16 @@ public function testCursorPaginatedModelCollectionRetrievalWhenNoElementsAndDefa
401401
$this->assertInstanceOf(CursorPaginator::class, $models);
402402
}
403403

404+
public function testFirstOrNew()
405+
{
406+
$user1 = EloquentTestUser::firstOrNew(
407+
['name' => 'Dries Vints'],
408+
['name' => 'Nuno Maduro']
409+
);
410+
411+
$this->assertSame('Nuno Maduro', $user1->name);
412+
}
413+
404414
public function testFirstOrCreate()
405415
{
406416
$user1 = EloquentTestUser::firstOrCreate(['email' => 'taylorotwell@gmail.com']);
@@ -425,6 +435,13 @@ public function testFirstOrCreate()
425435
$this->assertNotEquals($user3->id, $user1->id);
426436
$this->assertSame('abigailotwell@gmail.com', $user3->email);
427437
$this->assertSame('Abigail Otwell', $user3->name);
438+
439+
$user4 = EloquentTestUser::firstOrCreate(
440+
['name' => 'Dries Vints'],
441+
['name' => 'Nuno Maduro', 'email' => 'nuno@laravel.com']
442+
);
443+
444+
$this->assertSame('Nuno Maduro', $user4->name);
428445
}
429446

430447
public function testUpdateOrCreate()
@@ -678,6 +695,36 @@ public function testBasicModelHydration()
678695
$this->assertCount(1, $models);
679696
}
680697

698+
public function testFirstOrNewOnHasOneRelationShip()
699+
{
700+
$user1 = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
701+
$post1 = $user1->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']);
702+
703+
$this->assertSame('New Post', $post1->name);
704+
705+
$user2 = EloquentTestUser::create(['email' => 'abigailotwell@gmail.com']);
706+
$post = $user2->post()->create(['name' => 'First Post']);
707+
$post2 = $user2->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']);
708+
709+
$this->assertSame('First Post', $post2->name);
710+
$this->assertSame($post->id, $post2->id);
711+
}
712+
713+
public function testFirstOrCreateOnHasOneRelationShip()
714+
{
715+
$user1 = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
716+
$post1 = $user1->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']);
717+
718+
$this->assertSame('New Post', $post1->name);
719+
720+
$user2 = EloquentTestUser::create(['email' => 'abigailotwell@gmail.com']);
721+
$post = $user2->post()->create(['name' => 'First Post']);
722+
$post2 = $user2->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']);
723+
724+
$this->assertSame('First Post', $post2->name);
725+
$this->assertSame($post->id, $post2->id);
726+
}
727+
681728
public function testHasOnSelfReferencingBelongsToManyRelationship()
682729
{
683730
$user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);

0 commit comments

Comments
 (0)
0