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.8] Fix many to many sync results with custom pivot model #28416

Merged
merged 6 commits into from
May 5, 2019
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 @@ -4,10 +4,16 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Collection as BaseCollection;

trait InteractsWithPivotTable
{
/**
* @var Collection
*/
private $current;

/**
* Toggles a model (or models) from the parent.
*
Expand Down Expand Up @@ -88,9 +94,7 @@ public function sync($ids, $detaching = true)
// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->newPivotQuery()->pluck(
$this->relatedPivotKey
)->all();
$current = $this->getCurrent()->pluck($this->relatedPivotKey)->all();

$detach = array_diff($current, array_keys(
$records = $this->formatRecordsList($this->parseIds($ids))
Expand Down Expand Up @@ -213,7 +217,14 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
*/
protected function updateExistingPivotUsingCustomClass($id, array $attributes, $touch)
{
$updated = $this->newPivot([
$updated = $this->getCurrent()
->where($this->foreignPivotKey, $this->parent->{$this->parentKey})
->where($this->relatedPivotKey, $this->parseId($id))
->first()
->fill($attributes)
->isDirty();

$this->newPivot([
$this->foreignPivotKey => $this->parent->{$this->parentKey},
$this->relatedPivotKey => $this->parseId($id),
], true)->fill($attributes)->save();
Expand Down Expand Up @@ -634,4 +645,18 @@ protected function getTypeSwapValue($type, $value)
return $value;
}
}

/**
* Get the existing records.
*
* @return \Illuminate\Support\Collection
*/
protected function getCurrent()
{
return $this->current ?: $this->newPivotQuery()->get()->map(function ($record) {
$class = $this->using ? $this->using : Pivot::class;

return (new $class)->setRawAttributes((array) $record, true);
});
}
}
25 changes: 25 additions & 0 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,31 @@ public function test_custom_pivot_class()
$this->assertEquals(2, PostTagPivot::first()->tag_id);
}

public function test_custom_pivot_class_using_sync()
{
Carbon::setTestNow('2017-10-10 10:10:10');

$post = Post::create(['title' => Str::random()]);

$tag = TagWithCustomPivot::create(['name' => Str::random()]);

$results = $post->tagsWithCustomPivot()->sync([
$tag->id => ['flag' => 1],
]);

$this->assertNotEmpty($results['attached']);

$results = $post->tagsWithCustomPivot()->sync([
$tag->id => ['flag' => 1],
]);

$this->assertEmpty($results['updated']);

$results = $post->tagsWithCustomPivot()->sync([]);

$this->assertNotEmpty($results['detached']);
}

public function test_attach_method()
{
$post = Post::create(['title' => Str::random()]);
Expand Down