Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Relationship Events Laravel Package

chelout/laravel-relationship-events

Adds missing Eloquent relationship events to Laravel models. Use simple traits (HasOne/Many, BelongsTo/Many, Morph*) to listen for attach/detach/sync, saved/updated, and other relation lifecycle hooks with parent/related context and IDs/attributes.

View on GitHub
Deep Wiki
Context7

One To Many Relations:

hasMany

Country model have many User models

namespace App\Models;

use App\User;
use Chelout\RelationshipEvents\Concerns\HasManyEvents;
use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    use HasManyEvents;

    protected $fillable = [
        'name',
    ];

    public function users()
    {
        return $this->hasMany(User::class);
    }
}

Now we can use methods to assosiate Country with User and also update assosiated models.

// ...

// create user
$user = factory('App\User')->create();

// Getting random country
$country = App\Models\Country::inRandomOrder()->first();

// Saving user with specified country
// This will fire two events hasManySaving, hasManySaved
$country->users()->save($user);

// ...

Now we should listen our events, for example we can register event listners in model's boot method:

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

        /**
         * One To Many Relationship Events
         */

        static::hasManySaving(function ($parent, $related) {
            Log::info("Saving user's country {$parent->name}.");
        });

        static::hasManySaved(function ($parent, $related) {
            Log::info("User's country is now set to {$parent->name}.");
        });
    }
// ...

Available methods and events

HasMany::create (HasOneOrMany::create)

  • fires hasManyCreating, hasManyCreated
  • events have $parent and $related models

HasMany::save (HasOneOrMany::save)

  • fires hasManyCreating, hasManyCreated
  • events have $parent and $related models

HasMany::update (HasOneOrMany::update)

  • fires hasManyUpdating, hasManyUpdated
  • events have $parent model and $related Eloquent collection

Note: has additional query to get related Eloquent collection

belongsTo

There is also inverse relation User belongs to Country

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Chelout\RelationshipEvents\Concerns\HasBelongsToEvents;

class User extends Model
{
    use HasBelongsToEvents;

    /**
     * Get the country associated with the user.
     */
    public function country()
    {
        return $this->belongsTo(Country::class);
    }
}

Now we can use methods to assosiate, dissosiate and update Country with User:

// ...
 $country = App\Models\Country::inRandomOrder()->first();

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

// Assosiate user with country
// This will fire two events belongsToAssociating, belongsToAssociated
$user->country()->associate($country);
// ...

Now we should listen our events, for example we can register event listners in model's boot method:

// ...
    protected static function boot()
    {
        parent::boot();

        static::belongsToAssociating(function ($relation, $related, $parent) {
            Log::info("Associating country {$parent->name} with user.");
        });

        static::belongsToAssociated(function ($relation, $related, $parent) {
            Log::info("User has been assosiated with country {$parent->name}.");
        });
    }
// ...

Available methods and events

BelongsTo::associate

  • fires belongsToAssociating, belongsToAssociated
  • events have $relation name, $related model and $parent model or key(depends on BelongsTo::associate $model parametr).

Note: related model is dirty, should be saved after associating

BelongsTo::dissociate

  • fires belongsToAssociating, belongsToAssociated
  • events have $relation name, $related and $parent models.

Note: has additional query to get parent model Note: related model is dirty, should be saved after dissociating

BelongsTo::update

  • fires belongsToUpdating, belongsToUpdated
  • events have $relation name, $related and $parent models.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor