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

[8.x] syncWithPivotDefaults method #35644

merged 4 commits into from
Dec 18, 2020

Conversation

ddzobov
Copy link
Contributor

@ddzobov ddzobov commented Dec 17, 2020

This PR introduces ability to sync records with default values in pivot tables

$server = Server::find(1);
$ips = Ip::findMany([1,2,3]);

$server->ips()->syncWithPivotDefaults($ips, ['vlan' => 1444]);

@ddzobov ddzobov changed the title [8.x] syncWithPivotDefaults methos [8.x] syncWithPivotDefaults method Dec 17, 2020
…ithPivotTable.php

Co-authored-by: Dries Vints <dries@vints.io>
@ddzobov
Copy link
Contributor Author

ddzobov commented Dec 17, 2020

something wrong with tests, it fails not on my changes

@driesvints
Copy link
Member

@ddzobov I'm looking into that now. Feel free to ignore

@taylorotwell
Copy link
Member

How is this different than sync? Just so you don't have to pass the pivot attributes with each key?

@ddzobov
Copy link
Contributor Author

ddzobov commented Dec 17, 2020

How is this different than sync? Just so you don't have to pass the pivot attributes with each key?

If i want to do this via sync method, it will be something like this:

$server = Server::find(1);
$ips = Ip::findMany([1,2,3]);

$server->ips()->sync([
     1 => ['vlan' => 1444],
     2 => ['vlan' => 1444],
     3 => ['vlan' => 1444],
]);

// or
$defaults = ['vlan' => 1444];
$server->ips()->sync(
     $ips->mapWithKeys(fn($item) => [$item->getKey() => $defaults])->all()
);

@taylorotwell taylorotwell merged commit 5739cd5 into laravel:8.x Dec 18, 2020
@ddzobov ddzobov deleted the feature/sync-with-pivot-defaults branch December 18, 2020 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants