PHPackages                             chelout/laravel-relationship-events - 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. chelout/laravel-relationship-events

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

chelout/laravel-relationship-events
===================================

Missing relationship events for Laravel

v4.0.0(1y ago)5252.3M↓14.6%39[5 issues](https://github.com/chelout/laravel-relationship-events/issues)[4 PRs](https://github.com/chelout/laravel-relationship-events/pulls)17MITPHPPHP ^8.2CI passing

Since Mar 28Pushed 1mo ago12 watchersCompare

[ Source](https://github.com/chelout/laravel-relationship-events)[ Packagist](https://packagist.org/packages/chelout/laravel-relationship-events)[ Docs](https://github.com/chelout/laravel-relationship-events)[ RSS](/packages/chelout-laravel-relationship-events/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (38)Used By (17)

Laravel Relationship Events
===========================

[](#laravel-relationship-events)

Missing relationship events for Laravel

 [![Build Status](https://github.com/chelout/laravel-relationship-events/workflows/tests/badge.svg)](https://github.com/chelout/laravel-relationship-events/actions) [![Total Downloads](https://camo.githubusercontent.com/6f7ed681b5cbdf40db9140e210bec8cf6f6866399f88a698c27c52497016d3b6/68747470733a2f2f706f7365722e707567782e6f72672f6368656c6f75742f6c61726176656c2d72656c6174696f6e736869702d6576656e74732f642f746f74616c2e737667)](https://packagist.org/packages/chelout/laravel-relationship-events) [![Latest Stable Version](https://camo.githubusercontent.com/ffa2066cfd8b3cb565b5fe08ac6d807a60ded91ef3faa3686db9e12b6cbe076c/68747470733a2f2f706f7365722e707567782e6f72672f6368656c6f75742f6c61726176656c2d72656c6174696f6e736869702d6576656e74732f762f737461626c652e737667)](https://packagist.org/packages/chelout/laravel-relationship-events) [![License](https://camo.githubusercontent.com/a2ac56b9a0a4e49914e90ea79b47c83eca9edbed0221d28f0993fcc7e2c0199e/68747470733a2f2f706f7365722e707567782e6f72672f6368656c6f75742f6c61726176656c2d72656c6174696f6e736869702d6576656e74732f6c6963656e73652e737667)](https://packagist.org/packages/chelout/laravel-relationship-events)

Install
-------

[](#install)

1. Install package with composer

#### Stable branch:

[](#stable-branch)

```
composer require chelout/laravel-relationship-events

```

#### Development branch:

[](#development-branch)

```
composer require chelout/laravel-relationship-events:dev-master

```

2. Use necessary trait in your model.

#### Available traits:

[](#available-traits)

- HasOneEvents
- HasBelongsToEvents
- HasManyEvents
- HasBelongsToManyEvents
- HasMorphOneEvents
- HasMorphToEvents
- HasMorphManyEvents
- HasMorphToManyEvents
- HasMorphedByManyEvents

```
use Chelout\RelationshipEvents\Concerns\HasOneEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasOneEvents;

    public static function boot()
    {
        parent::boot();

        /**
         * One To One Relationship Events
         */
        static::hasOneSaved(function ($parent, $related) {
            dump('hasOneSaved', $parent, $related);
        });

        static::hasOneUpdated(function ($parent, $related) {
            dump('hasOneUpdated', $parent, $related);
        });
    }

}
```

```
use Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasMorphToManyEvents;

    public static function boot()
    {
        parent::boot();

        /**
         * Many To Many Polymorphic Relations Events.
         */
        static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) {
            dump('morphToManyAttached', $relation, $parent, $ids, $attributes);
        });

        static::morphToManyDetached(function ($relation, $parent, $ids) {
            dump('morphToManyDetached', $relation, $parent, $ids);
        });
    }

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }

}
```

3. Dispatchable relationship events. It is possible to fire event classes via $dispatchesEvents properties and adding `HasDispatchableEvents` trait:

```
use Chelout\RelationshipEvents\Concerns\HasOneEvents;
use Chelout\RelationshipEvents\Traits\HasDispatchableEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasDispatchableEvents;
    use HasOneEvents;

    protected $dispatchesEvents = [
        'hasOneSaved' => HasOneSaved::class,
    ];

}
```

Relationships
-------------

[](#relationships)

- [One To One Relations](doc/1-one-to-one.md)
- [One To Many Relations](doc/2-one-to-many.md)
- [Many To Many Relations](doc/3-many-to-many.md)
- [Has Many Through Relations](doc/4-has-many-through.md)
- [One To One Polymorphic Relations](doc/5-one-to-one-polymorphic.md)
- [One To Many Polymorphic Relations](doc/6-one-to-many-polymorphic.md)
- [Many To Many Polymorphic Relations](doc/7-many-to-many-polymorphic.md)

Observers
---------

[](#observers)

Starting from v0.4 it is possible to use relationship events in [Laravel observers classes](https://laravel.com/docs/5.6/eloquent#observers) Usage is very simple. Let's take `User` and `Profile` classes from [One To One Relations](doc/1-one-to-one.md), add `HasRelationshipObservables` trait to `User` class. Define observer class:

```
namespace App\Observer;

class UserObserver
{
    /**
     * Handle the User "hasOneCreating" event.
     *
     * @param \App\Models\User $user
     * @param \Illuminate\Database\Eloquent\Model $related
     *
     * @return void
     */
    public function hasOneCreating(User $user, Model $related)
    {
        Log::info("Creating profile for user {$related->name}.");
    }

    /**
     * Handle the User "hasOneCreated" event.
     *
     * @param \App\Models\User $user
     * @param \Illuminate\Database\Eloquent\Model $related
     *
     * @return void
     */
    public function hasOneCreated(User $user, Model $related)
    {
        Log::info("Profile for user {$related->name} has been created.");
    }
}
```

Don't forget to register an observer in the `boot` method of your `AppServiceProvider`:

```
namespace App\Providers;

use App\Models\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
// ...
    public function boot()
    {
        User::observe(UserObserver::class);
    }
// ...
}
```

And now just create profile for user:

```
// ...
$user = factory(User::class)->create([
    'name' => 'John Smith',
]);

// Create profile and assosiate it with user
// This will fire two events hasOneCreating, hasOneCreated
$user->profile()->create([
    'phone' => '8-800-123-45-67',
    'email' => 'user@example.com',
    'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// ...
```

Todo
----

[](#todo)

- Tests, tests, tests

###  Health Score

68

—

FairBetter than 100% of packages

Maintenance72

Regular maintenance activity

Popularity62

Solid adoption and visibility

Community33

Small or concentrated contributor base

Maturity87

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 84.5% 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 ~108 days

Recently: every ~405 days

Total

25

Last Release

367d ago

Major Versions

v0.6.3 → v1.0.02019-03-04

v1.5.0 → v2.0.02023-02-15

v2.0.0 → v3.0.02024-03-15

v3.0.0 → v4.0.02025-05-16

PHP version history (8 changes)0.1PHP &gt;=7.1.3

v0.3PHP &gt;=7.0.0

v0.3.2PHP ^7.0

v1.0.0PHP ^7.1.3

v1.1.0PHP ^7.2

1.4.0PHP ^7.2|^8.0

v2.0.0PHP ^8.1

v3.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/60cfb609b4fa19a2a1688987c6bdd70fe01d7b1cc3ab826593bb5997dc1b926d?d=identicon)[chelout](/maintainers/chelout)

---

Top Contributors

[![chelout](https://avatars.githubusercontent.com/u/9196257?v=4)](https://github.com/chelout "chelout (158 commits)")[![saleem-hadad](https://avatars.githubusercontent.com/u/12780866?v=4)](https://github.com/saleem-hadad "saleem-hadad (19 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (5 commits)")[![nhedger](https://avatars.githubusercontent.com/u/649677?v=4)](https://github.com/nhedger "nhedger (2 commits)")[![alvin-nt](https://avatars.githubusercontent.com/u/5227472?v=4)](https://github.com/alvin-nt "alvin-nt (1 commits)")[![giekus](https://avatars.githubusercontent.com/u/52037203?v=4)](https://github.com/giekus "giekus (1 commits)")[![waltoncon](https://avatars.githubusercontent.com/u/7566870?v=4)](https://github.com/waltoncon "waltoncon (1 commits)")

---

Tags

eventslaravelrelationsrelationshiplaraveleventsrelationsrelationship

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/chelout-laravel-relationship-events/health.svg)

```
[![Health](https://phpackages.com/badges/chelout-laravel-relationship-events/health.svg)](https://phpackages.com/packages/chelout-laravel-relationship-events)
```

###  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)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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