Skip to content

[9.x] Fix BelongsToMany#updateOrCreate behaviour merge arguments for new models #35827

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

Merged
merged 2 commits into from
Jan 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 23 additions & 10 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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()]);
Expand Down Expand Up @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand All @@ -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()]);

Expand Down Expand Up @@ -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()]);

Expand Down Expand Up @@ -1030,7 +1043,7 @@ class Tag extends Model
{
public $table = 'tags';
public $timestamps = true;
protected $fillable = ['name'];
protected $fillable = ['name', 'type'];

public function posts()
{
Expand Down