PHPackages                             alfa/laravel-has-many-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. alfa/laravel-has-many-sync

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alfa/laravel-has-many-sync
==========================

Laravel has many sync

1.1.0(7y ago)161811[1 issues](https://github.com/alfa6661/laravel-hasmany-sync/issues)[2 PRs](https://github.com/alfa6661/laravel-hasmany-sync/pulls)MITPHP

Since Aug 10Pushed 5y ago3 watchersCompare

[ Source](https://github.com/alfa6661/laravel-hasmany-sync)[ Packagist](https://packagist.org/packages/alfa/laravel-has-many-sync)[ RSS](/packages/alfa-laravel-has-many-sync/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

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

[](#laravel-hasmany-sync)

Allow sync method for Laravel Has Many Relationship.

Installation
============

[](#installation)

You can install the package via composer:

```
composer require alfa6661/laravel-has-many-sync

```

Register the ServiceProvider in `config/app.php`

```
'providers' => [
    // ...
    Alfa6661\EloquentHasManySync\ServiceProvider::class,
],
```

Usage
=====

[](#usage)

Setup HasMany Relation
----------------------

[](#setup-hasmany-relation)

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

You can access the sync method like this:

```
$customer->contacts()->sync([
    [
        'id' => 1,
        'name' => 'Alfa',
        'phone_number' => '123',
    ],
    [
        'id' => null,
        'name' => 'Adhitya',
        'phone_number' => '234,
    ]
]);
```

The sync method accepts an array of data to place on the intermediate table. Any data that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the data in the given array will exist in the intermediate table:

### Syncing without deleting

[](#syncing-without-deleting)

If you do not want to delete existing data, you may pass false value to the second parameter in the sync method.

```
$customer->contacts()->sync([
    [
        'id' => 1,
        'name' => 'Alfa',
        'phone_number' => '123',
    ],
    [
        'id' => null,
        'name' => 'Adhitya',
        'phone_number' => '234,
    ]
], false);
```

### Example usage in the controller.

[](#example-usage-in-the-controller)

```
class CustomersController extends Controller
{
    /**
     * Update the specified resource in storage.
     *
     * @param  CustomerRequest  $request
     * @param  Customer $customer
     * @return \Illuminate\Http\Response
     */
    public function update(CustomerRequest $request, Customer $customer)
    {
        DB::transaction(function () use ($customer, $request) {
            $customer->update($request->all());
            $customer->contacts()->sync($request->get('contacts', []));
        });

        return redirect()->route('customers.index');
    }
}
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~3 days

Total

2

Last Release

2829d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ef0480adb6c2494f21910eb4e69ab92b02e075bfdad37c242fed4f20d0e5412c?d=identicon)[alfa6661](/maintainers/alfa6661)

---

Top Contributors

[![alfa6661](https://avatars.githubusercontent.com/u/3650559?v=4)](https://github.com/alfa6661 "alfa6661 (4 commits)")

### Embed Badge

![Health badge](/badges/alfa-laravel-has-many-sync/health.svg)

```
[![Health](https://phpackages.com/badges/alfa-laravel-has-many-sync/health.svg)](https://phpackages.com/packages/alfa-laravel-has-many-sync)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

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