Skip to content

Commit 4c0d55f

Browse files
authored
[9.x] Fix BelongsToMany#updateOrCreate behaviour merge arguments for new models (#35827)
* Update updateOrCreate on BelongsToMany to follow default behaviour of merging arguments for newly created models * Fix failing tests Co-authored-by: Marco <marco@dcc.team>
1 parent ce57929 commit 4c0d55f

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ public function firstOrCreate(array $attributes, array $joining = [], $touch = t
628628
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
629629
{
630630
if (is_null($instance = $this->related->where($attributes)->first())) {
631-
return $this->create($values, $joining, $touch);
631+
return $this->create(array_merge($attributes, $values), $joining, $touch);
632632
}
633633

634634
$instance->fill($values);

tests/Integration/Database/EloquentBelongsToManyTest.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected function setUp(): void
3939
Schema::create('tags', function (Blueprint $table) {
4040
$table->increments('id');
4141
$table->string('name');
42+
$table->string('type')->nullable();
4243
$table->timestamps();
4344
});
4445

@@ -453,6 +454,18 @@ public function testUpdateOrCreateMethod()
453454
$this->assertNotNull($post->tags()->whereName('dives')->first());
454455
}
455456

457+
public function testUpdateOrCreateMethodCreate()
458+
{
459+
$post = Post::create(['title' => Str::random()]);
460+
461+
$post->tags()->updateOrCreate(['name' => 'wavez'], ['type' => 'featured']);
462+
463+
$tag = $post->tags()->whereType('featured')->first();
464+
465+
$this->assertNotNull($tag);
466+
$this->assertSame('wavez', $tag->name);
467+
}
468+
456469
public function testSyncMethod()
457470
{
458471
$post = Post::create(['title' => Str::random()]);
@@ -647,7 +660,7 @@ public function testCanTouchRelatedModels()
647660

648661
public function testWherePivotOnString()
649662
{
650-
$tag = Tag::create(['name' => Str::random()]);
663+
$tag = Tag::create(['name' => Str::random()])->fresh();
651664
$post = Post::create(['title' => Str::random()]);
652665

653666
DB::table('posts_tags')->insert([
@@ -663,7 +676,7 @@ public function testWherePivotOnString()
663676

664677
public function testFirstWhere()
665678
{
666-
$tag = Tag::create(['name' => 'foo']);
679+
$tag = Tag::create(['name' => 'foo'])->fresh();
667680
$post = Post::create(['title' => Str::random()]);
668681

669682
DB::table('posts_tags')->insert([
@@ -679,7 +692,7 @@ public function testFirstWhere()
679692

680693
public function testWherePivotOnBoolean()
681694
{
682-
$tag = Tag::create(['name' => Str::random()]);
695+
$tag = Tag::create(['name' => Str::random()])->fresh();
683696
$post = Post::create(['title' => Str::random()]);
684697

685698
DB::table('posts_tags')->insert([
@@ -695,7 +708,7 @@ public function testWherePivotOnBoolean()
695708

696709
public function testWherePivotInMethod()
697710
{
698-
$tag = Tag::create(['name' => Str::random()]);
711+
$tag = Tag::create(['name' => Str::random()])->fresh();
699712
$post = Post::create(['title' => Str::random()]);
700713

701714
DB::table('posts_tags')->insert([
@@ -730,7 +743,7 @@ public function testOrWherePivotInMethod()
730743
public function testWherePivotNotInMethod()
731744
{
732745
$tag1 = Tag::create(['name' => Str::random()]);
733-
$tag2 = Tag::create(['name' => Str::random()]);
746+
$tag2 = Tag::create(['name' => Str::random()])->fresh();
734747
$post = Post::create(['title' => Str::random()]);
735748

736749
DB::table('posts_tags')->insert([
@@ -768,7 +781,7 @@ public function testOrWherePivotNotInMethod()
768781
public function testWherePivotNullMethod()
769782
{
770783
$tag1 = Tag::create(['name' => Str::random()]);
771-
$tag2 = Tag::create(['name' => Str::random()]);
784+
$tag2 = Tag::create(['name' => Str::random()])->fresh();
772785
$post = Post::create(['title' => Str::random()]);
773786

774787
DB::table('posts_tags')->insert([
@@ -784,7 +797,7 @@ public function testWherePivotNullMethod()
784797

785798
public function testWherePivotNotNullMethod()
786799
{
787-
$tag1 = Tag::create(['name' => Str::random()]);
800+
$tag1 = Tag::create(['name' => Str::random()])->fresh();
788801
$tag2 = Tag::create(['name' => Str::random()]);
789802
$post = Post::create(['title' => Str::random()]);
790803

@@ -909,8 +922,8 @@ public function testPivotDoesntHavePrimaryKey()
909922
public function testOrderByPivotMethod()
910923
{
911924
$tag1 = Tag::create(['name' => Str::random()]);
912-
$tag2 = Tag::create(['name' => Str::random()]);
913-
$tag3 = Tag::create(['name' => Str::random()]);
925+
$tag2 = Tag::create(['name' => Str::random()])->fresh();
926+
$tag3 = Tag::create(['name' => Str::random()])->fresh();
914927
$tag4 = Tag::create(['name' => Str::random()]);
915928
$post = Post::create(['title' => Str::random()]);
916929

@@ -1030,7 +1043,7 @@ class Tag extends Model
10301043
{
10311044
public $table = 'tags';
10321045
public $timestamps = true;
1033-
protected $fillable = ['name'];
1046+
protected $fillable = ['name', 'type'];
10341047

10351048
public function posts()
10361049
{

0 commit comments

Comments
 (0)