PHPackages                             f-liva/laravel-hasmany-sync - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. f-liva/laravel-hasmany-sync

ActiveLibrary[Database &amp; ORM](/categories/database)

f-liva/laravel-hasmany-sync
===========================

Sync Laravel HasMany relationships with ease

1.2.2(1mo ago)15422MITPHPPHP ^7.1|^8

Since Nov 15Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/f-liva/laravel-hasmany-sync)[ Packagist](https://packagist.org/packages/f-liva/laravel-hasmany-sync)[ Docs](https://github.com/f-liva/laravel-hasmany-sync)[ RSS](/packages/f-liva-laravel-hasmany-sync/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (12)Versions (6)Used By (0)

Laravel HasMany Sync
====================

[](#laravel-hasmany-sync)

[![Latest Version on Packagist](https://camo.githubusercontent.com/246d86c3d24b23b405f896920149ec896148ae4c3f6ced7a3980d6fc168854dc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666c6976612f6c61726176656c2d6861736d616e792d73796e633f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fliva/laravel-hasmany-sync)[![License](https://camo.githubusercontent.com/0841b9c3086789ad1f829362a58394eddd111af58ee71093ed316b2557db909c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f662d6c6976612f6c61726176656c2d6861736d616e792d73796e633f7374796c653d666c61742d737175617265)](license.md)[![Codecov](https://camo.githubusercontent.com/3ec2488a471112b4f8f3f7e34de1d502a502b59cf7a672ad81fae79c5ab8033e/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f662d6c6976612f6c61726176656c2d6861736d616e792d73796e633f7374796c653d666c61742d737175617265)](https://codecov.io/gh/f-liva/laravel-hasmany-sync)[![TravisCI](https://camo.githubusercontent.com/fffc6ff554ed26ade969f6e2e1c1f8c605acf6603ade7bbc2dac2b1a05638809/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f662d6c6976612f6c61726176656c2d6861736d616e792d73796e633f7374796c653d666c61742d737175617265)](https://travis-ci.org/f-liva/laravel-hasmany-sync)[![StyleCI](https://camo.githubusercontent.com/9b21b7167b5708ec456ede541d506c7d7eac8370dff0a2a954ec9a5140052a9b/68747470733a2f2f7374796c6563692e696f2f7265706f732f3230323430303432352f736869656c64)](https://styleci.io/repos/202400425)

With this package you will be able to synchronize your **HasMany** relationships just as you normally would for **BelongsToMany** relationships. The usage is the same, follow the official Laravel documentation.

Thanks to [korridor/laravel-has-many-sync](https://github.com/korridor/laravel-has-many-sync) for the original idea. This package reconstructs the mechanism using the original Laravel approach and operation.

Installation
------------

[](#installation)

You can install the package via composer with following command:

```
composer require f-liva/laravel-hasmany-sync
```

Usage
-----

[](#usage)

Configure your **HasMany** relationship as you normally would

```
class Customer extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function contacts()
    {
        return $this->hasMany(CustomerContact::class);
    }
}
```

Synchronize the relationship as you normally would in a **BelongsToMany** relationship

```
$customer->contacts()->sync([1, 2, 3]);

// Or with attributes...

$customer->contacts()->sync([1 => ['name' => 'Foo'], 2 => ['name' => 'Bar'], 3]);
```

The sync method accepts the same parameters described in [Eloquent Relationships - Syncing Relations](https://laravel.com/docs/9.x/eloquent-relationships#syncing-associations)

You can also synchronize the relationship without knowing the identifiers of the related records. In this case, specify in the call the `syncRelatedKey: false` parameter.

```
$customer->contacts()->sync([1, 2, 3], syncRelatedKey: false);

// Or with attributes...

$customer->contacts()->sync([['name' => 'Foo'], ['name' => 'Bar'], ...]);
```

Contributing
------------

[](#contributing)

I am open for suggestions and contributions. Just create an issue or a pull request.

### Testing

[](#testing)

The `composer test` command runs all tests with [phpunit](https://phpunit.de/). The `composer test-coverage` command runs all tests with phpunit and creates a coverage report into the `coverage` folder.

### Codeformatting/Linting

[](#codeformattinglinting)

The `composer fix` command formats the code with [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). The `composer lint` command checks the code with [phpcs](https://github.com/PHPCSStandards/PHP_CodeSniffer).

License
-------

[](#license)

This package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance89

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~306 days

Total

5

Last Release

56d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/796e4d6545ebd619068a5905951de016ca61799e96c918f62a8659d1c04af1af?d=identicon)[f-liva](/maintainers/f-liva)

---

Top Contributors

[![f-liva](https://avatars.githubusercontent.com/u/346224?v=4)](https://github.com/f-liva "f-liva (2 commits)")[![rodrigoprimo](https://avatars.githubusercontent.com/u/77215?v=4)](https://github.com/rodrigoprimo "rodrigoprimo (1 commits)")

---

Tags

laraveleloquentrelationssynchasmany

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/f-liva-laravel-hasmany-sync/health.svg)

```
[![Health](https://phpackages.com/badges/f-liva-laravel-hasmany-sync/health.svg)](https://phpackages.com/packages/f-liva-laravel-hasmany-sync)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[korridor/laravel-has-many-sync

Laravel has many sync

3578.1k](/packages/korridor-laravel-has-many-sync)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
