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

[5.6] Add tests for updating existing pivot + add option for passing model #23535

Merged
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 @@ -188,7 +188,7 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
$attributes = $this->addTimestampsToAttachment($attributes, true);
}

$updated = $this->newPivotStatementForId($id)->update(
$updated = $this->newPivotStatementForId($this->parseId($id))->update(
$this->castAttributes($attributes)
);

Expand Down Expand Up @@ -488,6 +488,21 @@ protected function parseIds($value)
return (array) $value;
}

/**
* Get the ID from the given mixed value.
*
* @param mixed $value
* @return mixed
*/
protected function parseId($value)
{
if ($value instanceof Model) {
return $value->getKey();
}

return $value;
}

/**
* Cast the given keys to integers if they are numeric and string otherwise.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,44 @@ public function can_touch_related_models()

$this->assertNotEquals('2017-10-10 10:10:10', Tag::find(300)->updated_at);
}

/**
* @test
*/
public function can_update_existing_pivot()
{
$tag = Tag::create(['name' => str_random()]);
$post = Post::create(['title' => str_random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'empty'],
]);

$post->tagsWithExtraPivot()->updateExistingPivot($tag->id, ['flag' => 'exclude']);

foreach ($post->tagsWithExtraPivot as $tag) {
$this->assertEquals('exclude', $tag->pivot->flag);
}
}

/**
* @test
*/
public function can_update_existing_pivot_using_model()
{
$tag = Tag::create(['name' => str_random()]);
$post = Post::create(['title' => str_random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'empty'],
]);

$post->tagsWithExtraPivot()->updateExistingPivot($tag, ['flag' => 'exclude']);

foreach ($post->tagsWithExtraPivot as $tag) {
$this->assertEquals('exclude', $tag->pivot->flag);
}
}
}

class Post extends Model
Expand All @@ -601,6 +639,12 @@ public function tags()
->wherePivot('flag', '<>', 'exclude');
}

public function tagsWithExtraPivot()
{
return $this->belongsToMany(Tag::class, 'posts_tags', 'post_id', 'tag_id')
->withPivot('flag');
}

public function touchingTags()
{
return $this->belongsToMany(TouchingTag::class, 'posts_tags', 'post_id', 'tag_id')
Expand Down