elbgoods/eloquent-sync-one-to-many

0.3.0 2020-04-08 12:16 UTC

This package is auto-updated.

Last update: 2024-03-08 20:31:10 UTC


README

This package provides the sync function for one to many relations similar to the sync method from BelongsToMany.

Installation

You can install the package via composer:

composer require elbgoods/eloquent-sync-one-to-many

Usage

Sync using ids

$user->tasks()->sync([1, 2, 4]);

Sync using ids and additional attributes

$user->tasks()->sync([
    1 => ['status' => 'wip', 'priority' => 1],
    4 => ['status' => 'finished', 'priority' => 3],
]);

Sync without detaching

$user->tasks()->syncWithoutDetaching([1, 2, 4]);

// or

$user->tasks()->sync([1, 2, 4], ['detaching' => false]);

Sync and set additional attributes to detached

$user->tasks()->sync(
    [1, 2, 4],
    'set_after_detach' => [
        'status' => 'open',
        'priority' => 0,
    ],
);

Sync using values by given key as ids

$user->tasks()->sync([
    ['task_id' => 1, 'status' => 'wip', 'priority' => 1],
    ['task_id' => 4, 'status' => 'finished', 'priority' => 3],
], [foreign_id_key => 'task_id']);

Result is the same as the result of the sync method of BelongsToMany, an array with attach, detached and updated rows.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

Please see SECURITY for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Treeware

You're free to use this package, but if it makes it to your production environment we would highly appreciate you buying or planting the world a tree.

It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you contribute to my forest you’ll be creating employment for local families and restoring wildlife habitats.

You can buy trees at https://offset.earth/treeware

Read more about Treeware at https://treeware.earth

We offset our carbon footprint via Offset Earth

This package was generated using the Laravel Package Boilerplate.