Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravel-relationship-events

> 后端框架
Open source

Missing relationship events for Laravel

526 stars0 likes0 views
WebsiteGitHub

About

Missing relationship events for Laravel

Laravel Relationship Events

Missing relationship events for Laravel

Compatibility

Package Laravel PHP
v5.x 13.x ^8.3
v4.x 12.x ^8.2
v3.x 11.x ^8.2
v2.x 10.x ^8.1
v1.x 6.x - 9.x ^7.2 | ^8.0

Install

  1. Install package with composer

Stable branch:

composer require chelout/laravel-relationship-events

Development branch:

composer require chelout/laravel-relationship-events:dev-master
  1. Use necessary trait in your model.

Available traits:

  • HasOneEvents
  • HasBelongsToEvents
  • HasManyEvents
  • HasBelongsToManyEvents
  • HasMorphOneEvents
  • HasMorphToEvents
  • HasMorphManyEvents
  • HasMorphToManyEvents
  • HasMorphedByManyEvents
php

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);
        });
    }

}
…
  1. Dispatchable relationship events. It is possible to fire event classes via $dispatchesEvents properties and adding HasDispatchableEvents trait:
php

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

  • One To One Relations
  • One To Many Relations
  • Many To Many Relations
  • Has Many Through Relations
  • One To One Polymorphic Relations
  • One To Many Polymorphic Relations
  • Many To Many Polymorphic Relations

Observers

Starting from v0.4 it is possible to use relationship events in Laravel observers classes Usage is very simple. Let's take User and Profile classes from One To One Relations, add HasRelationshipObservables trait to User class. Define observer class:

…

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

php
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:

php
// ...
$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' => '[email protected]',
    'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// ...

Todo

  • Tests, tests, tests

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPeventslaravelrelationsrelationship

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架