Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brnlbs's avatar

Eloquent "Attach" - Which event is fired?

I recently switched from Numeric IDs to UUIDs in one of my projects and a weird problem occured. All my entites extend my base entity which adds the UUID in the boot method when the creating event is fired by Laravel.

Adding the ID works fine this way when I create entries directly via save/create/insert but when I use something like attach it won't add an ID which of course results in an error because the ID is empty when the entry gets saved into the database.

The tables that have this problem are only the pivot tables which only contain 3 fields each -> id, first_table_id, second_table_id.

So how would I add an ID to an entry of a pivot table that is created via attach/sync since there is no model for the pivot table itself?

$group = App::make('App\Repositories\GroupRepository')->getByID('some-random-uuid');
$user->groups()->attach($group);
public $incrementing = false;

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

    static::creating(function($model)
    {
       $model->{$model->getKeyName()} = \Rhumsaa\Uuid\Uuid::uuid4();
    });
}
0 likes
9 replies
brnlbs's avatar
brnlbs
OP
Best Answer
Level 1

Solved it that way a few days ago. This solutions fires for each pivot but for my current situation this works.

use Illuminate\Database\Eloquent\Relations\Pivot;

Pivot::creating(function($pivot) {
    $pivot->id = (string)$this->generateNewId();
});
2 likes
smenzer's avatar

Where does the code posted above go? Within the protected static function boot() function?

speccode's avatar

Above code should go to your App\Providers\AppServiceProvider.php into boot() function or even better to App\Providers\EventServiceProvider.php

~~Pivot class extends Model class so you can assume that all models events (like creating, created, updated etc.) are available.~~ After some struggling looks like only available events are: updating, updated, saving, saved. Where "saved" will not fire on when you attaching new Pivot.

Like @brnlbs said this event will be fired when you do something with any Pivot model. To be more precise I checked what is current pivot table like this:

    //App/Providers/EventServiceProvider.php
    public function boot()
    {
        Pivot::updated(function($pivot) {
            if ($pivot->getTable() == 'post_tags') {
                //do your stuff
                //ugly hack to get current parent model
                $pivot->offsetGet('parent')->touch();
            }
        });
}

Tested on Laravel 5.1

highbridgecreative's avatar

I too have the same issue. I put @codydh 's code in the AppServiceProvider, and EventServiceProvider's boot() method. I did it before and after parent::boot($events);. nothing seems to work.

Tested and failed on Laravel 5.1

After reading @speccode 's post,

After some struggling looks like only available events are: updating, updated, saving, saved. Where "saved" will not fire on when you attaching new Pivot

Does this mean that Pivot will not fire for created? If so, that's completely useless. A pivot table should ideally only have 2 primary-foreign keys in it. Which means it should only ever be deleted and/or added. and never updated.

2 likes
Kaylakaze-FE's avatar

For anyone looking at this now, I'm looking at the 5.2 code and it seems that relationships do not use the pivot model, which is why Pivot events do not fire. The relationship classes use direct DB queries to insert, delete, and update the pivot tables. I'm guessing that it's the same for future versions as well.

realtebo's avatar

@fico7489 : my goal is intercept events to register created_by, updated_by, and deleted_by.

Is there a way to make your package comunicate with the 'revisionable' package?

Please or to participate in this conversation.