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

[8.x] syncWithPivotDefaults method #35644

Merged
merged 4 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -123,6 +123,23 @@ public function sync($ids, $detaching = true)
return $changes;
}

/**
* Sync the intermediate tables with a list of IDs or collection of models with default values.
*
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
* @param bool $detaching
* @param array $defaults
ddzobov marked this conversation as resolved.
Show resolved Hide resolved
* @return array
*/
public function syncWithPivotDefaults($ids, array $defaults, bool $detaching = true)
{
$idsWithDefaults = collect($this->parseIds($ids))->mapWithKeys(function ($id) use ($defaults) {
return [$id => $defaults];
});

return $this->sync($idsWithDefaults, $detaching);
}

/**
* Format the sync / toggle record list so that it is keyed by ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function createSchema()
$table->foreign('article_id')->references('id')->on('articles');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->boolean('visible')->default(false);
});
}

Expand Down Expand Up @@ -87,6 +88,28 @@ public function testSyncReturnValueType()
collect($changes['attached'])->map(function ($id) {
$this->assertSame(gettype($id), (new BelongsToManySyncTestTestArticle)->getKeyType());
});

$user->articles->each(function (BelongsToManySyncTestTestArticle $article) {
$this->assertSame('0', $article->pivot->visible);
});
}

public function testSyncWithPivotDefaultsReturnValueType()
{
$this->seedData();

$user = BelongsToManySyncTestTestUser::query()->first();
$articleIDs = BelongsToManySyncTestTestArticle::all()->pluck('id')->toArray();

$changes = $user->articles()->syncWithPivotDefaults($articleIDs, ['visible' => true]);

collect($changes['attached'])->each(function ($id) {
$this->assertSame(gettype($id), (new BelongsToManySyncTestTestArticle)->getKeyType());
});

$user->articles->each(function (BelongsToManySyncTestTestArticle $article) {
$this->assertSame('1', $article->pivot->visible);
});
}

/**
Expand Down Expand Up @@ -118,7 +141,7 @@ class BelongsToManySyncTestTestUser extends Eloquent

public function articles()
{
return $this->belongsToMany(BelongsToManySyncTestTestArticle::class, 'article_user', 'user_id', 'article_id');
return $this->belongsToMany(BelongsToManySyncTestTestArticle::class, 'article_user', 'user_id', 'article_id')->withPivot('visible');
}
}

Expand Down