laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

syncMany support #2470

Open noecs opened 3 years ago

noecs commented 3 years ago

When I use Eloquent saveMany, I am thinking if a product has many sku, and every time I save, whether it can be like a many-to-many relationship, I can use sync to synchronize the one-to-many relationship . It looks like this:

$product->sku()->syncMany([
     [
         ['skuid' => '123123'],     // Attached table filter
         [
             'status' => true           // Fields that need to be updated
             'stock' => 100,
         ]
     ],
     ....
])
danilopolani commented 3 years ago

I did not quite understood the request. First of all, sync is for many-to-many relationships, not for one-to-many and it already supports additional fields to be updated on the pivot table (code taken from the docs linked before):

$user->roles()->sync([1 => ['expires' => true], 2, 3]);

Therefore your code could be:

$product->sku()->sync([
    '123123' => [
        'status' => true,
        'stock' => 100,
    ],
]);