PHPackages                             nylontechnology/belongstomanywithevents - 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. nylontechnology/belongstomanywithevents

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

nylontechnology/belongstomanywithevents
=======================================

Override of Laravel Eloquent BelongsToMany relationship to fire events on attach/detach operations

18PHP

Since Mar 25Pushed 5y ago3 watchersCompare

[ Source](https://github.com/NylonTechnology/BelongsToManyWithEvents)[ Packagist](https://packagist.org/packages/nylontechnology/belongstomanywithevents)[ RSS](/packages/nylontechnology-belongstomanywithevents/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

BelongsToManyWithEvents
=======================

[](#belongstomanywithevents)

Override of Laravel Eloquent BelongsToMany relationship to fire events on attach/detach operations

Install
-------

[](#install)

```
$ composer require nylontechnology/belongstomanywithevents

```

Usage
-----

[](#usage)

1. Import into your model

```
use NylonTechnology\BelongsToManyWithEvents as BelongsToMany;

class User extends Model {
....
```

2. In your model, define what relationship events you want to observe

```
class User extends Model {

    protected $observables = [
        'attached.roles',
        'detached.roles'
    ];

	public function roles()
	{
		return $this->belongsToMany('App\Role');
	}

	...
```

3. Create an observer and handle events however you wish

```
namespace App\Observers;

class ModelAuditObserver {

    private function attached($observer_callback, $model, $ids) {}

		private function detached($observer_callback, $model, $ids) {}

		private function synced($observer_callback, $model, $ids) {}

		...
```

4. Register your observer in AppServiceProvider boot() method

```
class AppServiceProvider extends ServiceProvider {

    public function boot() {
        App\User::observe(ModelAuditObserver::class);

    ...
```

Notes
-----

[](#notes)

sync() is a wrapper around attach() and detach() so generally you should only observe sync events or attach/detach, but not both unless you don't mind redundant events.

Inspired by [@andyberry88's gist](https://gist.github.com/andyberry88/be3c45380568fc359cb61e00c4249704)

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 90% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f48e63e5da345534bf07c3784a32338ad224b52d6396109be7041e9eda1db77?d=identicon)[stpangia](/maintainers/stpangia)

---

Top Contributors

[![stpangia](https://avatars.githubusercontent.com/u/716278?v=4)](https://github.com/stpangia "stpangia (27 commits)")[![seanpatches](https://avatars.githubusercontent.com/u/9610575?v=4)](https://github.com/seanpatches "seanpatches (2 commits)")[![nylonjames](https://avatars.githubusercontent.com/u/44171142?v=4)](https://github.com/nylonjames "nylonjames (1 commits)")

### Embed Badge

![Health badge](/badges/nylontechnology-belongstomanywithevents/health.svg)

```
[![Health](https://phpackages.com/badges/nylontechnology-belongstomanywithevents/health.svg)](https://phpackages.com/packages/nylontechnology-belongstomanywithevents)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M543](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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