diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index ab13ef538fcd..cecb13e915dc 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -628,7 +628,7 @@ public function firstOrCreate(array $attributes, array $joining = [], $touch = t public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true) { if (is_null($instance = $this->related->where($attributes)->first())) { - return $this->create($values, $joining, $touch); + return $this->create(array_merge($attributes, $values), $joining, $touch); } $instance->fill($values); diff --git a/tests/Integration/Database/EloquentBelongsToManyTest.php b/tests/Integration/Database/EloquentBelongsToManyTest.php index b66cd3d3f34c..2599fdd85858 100644 --- a/tests/Integration/Database/EloquentBelongsToManyTest.php +++ b/tests/Integration/Database/EloquentBelongsToManyTest.php @@ -39,6 +39,7 @@ protected function setUp(): void Schema::create('tags', function (Blueprint $table) { $table->increments('id'); $table->string('name'); + $table->string('type')->nullable(); $table->timestamps(); }); @@ -453,6 +454,18 @@ public function testUpdateOrCreateMethod() $this->assertNotNull($post->tags()->whereName('dives')->first()); } + public function testUpdateOrCreateMethodCreate() + { + $post = Post::create(['title' => Str::random()]); + + $post->tags()->updateOrCreate(['name' => 'wavez'], ['type' => 'featured']); + + $tag = $post->tags()->whereType('featured')->first(); + + $this->assertNotNull($tag); + $this->assertSame('wavez', $tag->name); + } + public function testSyncMethod() { $post = Post::create(['title' => Str::random()]); @@ -647,7 +660,7 @@ public function testCanTouchRelatedModels() public function testWherePivotOnString() { - $tag = Tag::create(['name' => Str::random()]); + $tag = Tag::create(['name' => Str::random()])->fresh(); $post = Post::create(['title' => Str::random()]); DB::table('posts_tags')->insert([ @@ -663,7 +676,7 @@ public function testWherePivotOnString() public function testFirstWhere() { - $tag = Tag::create(['name' => 'foo']); + $tag = Tag::create(['name' => 'foo'])->fresh(); $post = Post::create(['title' => Str::random()]); DB::table('posts_tags')->insert([ @@ -679,7 +692,7 @@ public function testFirstWhere() public function testWherePivotOnBoolean() { - $tag = Tag::create(['name' => Str::random()]); + $tag = Tag::create(['name' => Str::random()])->fresh(); $post = Post::create(['title' => Str::random()]); DB::table('posts_tags')->insert([ @@ -695,7 +708,7 @@ public function testWherePivotOnBoolean() public function testWherePivotInMethod() { - $tag = Tag::create(['name' => Str::random()]); + $tag = Tag::create(['name' => Str::random()])->fresh(); $post = Post::create(['title' => Str::random()]); DB::table('posts_tags')->insert([ @@ -730,7 +743,7 @@ public function testOrWherePivotInMethod() public function testWherePivotNotInMethod() { $tag1 = Tag::create(['name' => Str::random()]); - $tag2 = Tag::create(['name' => Str::random()]); + $tag2 = Tag::create(['name' => Str::random()])->fresh(); $post = Post::create(['title' => Str::random()]); DB::table('posts_tags')->insert([ @@ -768,7 +781,7 @@ public function testOrWherePivotNotInMethod() public function testWherePivotNullMethod() { $tag1 = Tag::create(['name' => Str::random()]); - $tag2 = Tag::create(['name' => Str::random()]); + $tag2 = Tag::create(['name' => Str::random()])->fresh(); $post = Post::create(['title' => Str::random()]); DB::table('posts_tags')->insert([ @@ -784,7 +797,7 @@ public function testWherePivotNullMethod() public function testWherePivotNotNullMethod() { - $tag1 = Tag::create(['name' => Str::random()]); + $tag1 = Tag::create(['name' => Str::random()])->fresh(); $tag2 = Tag::create(['name' => Str::random()]); $post = Post::create(['title' => Str::random()]); @@ -909,8 +922,8 @@ public function testPivotDoesntHavePrimaryKey() public function testOrderByPivotMethod() { $tag1 = Tag::create(['name' => Str::random()]); - $tag2 = Tag::create(['name' => Str::random()]); - $tag3 = Tag::create(['name' => Str::random()]); + $tag2 = Tag::create(['name' => Str::random()])->fresh(); + $tag3 = Tag::create(['name' => Str::random()])->fresh(); $tag4 = Tag::create(['name' => Str::random()]); $post = Post::create(['title' => Str::random()]); @@ -1030,7 +1043,7 @@ class Tag extends Model { public $table = 'tags'; public $timestamps = true; - protected $fillable = ['name']; + protected $fillable = ['name', 'type']; public function posts() {