Home » Php » php – model event "deleted" does not return all pivot attributes

php – model event "deleted" does not return all pivot attributes

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a many-to-many relationship between User and Role models.

I want to save a history (logging) in my database every time I attach or detach a user to a role.

$role->users()->attach(...);

Since Laravel can’t fire model events by default on pivot tables, I found I had to create my own custom pivot model. So here is what I did so far :

inside Role model :

    public function users(): MorphToMany
    {
        return $this
            ->morphedByMany(...)
            ->using(\App\Pivot\RolePivot::class) // <---------- NOTICE THIS
            ->withTimestamps();
    }

inside RolePivot model :

class RolePivot extends MorphPivot
{
    use RecordsModelHistory;
}

inside my RecordsModelHistory trait :

    protected static function bootRecordsModelHistory()
    {
        static::historisableModelEvents()->each(function ($event) {
            return static::$event(function (Model $model) use ($event) {
                /** @var self $model */
                if($event==='deleted'){dd($model);} // <---------- NOTICE THIS
                if (! $model->shouldRecordModelHistory($event)) {
                    return;
                }

                $model->saveModelHistory($event);
            });
        });
    }

the dd($model) is executed when I perform $role->users()->detach(...);. So far so good, since, indeed, detaching a user means deleting a row from the pivot table.

My problem is the following: the $model->attributes do not contain all the attributes I expected to see.

I only have role_id and model_id : https://i.imgur.com/17Q3gTk.png

eventhough my pivot table model_has_roles has columns model_type, model_id, role_id, created_at, updated_at : https://i.imgur.com/cx0VA8B.png

Why does it only return role_id and model_id, while I’d expect +3 more attributes?

Oddly enough, I successfully get all 5 attributes (aka pivot table’s 5 columns) when attaching (event = created), but only 3 attributes when detaching (event = deleted)

How to&Answers: