Zakaria Acharki
Zakaria Acharki

Reputation: 67525

Laravel model event won't fire

I'm using ajax to update my model User, the ajax part works fine since the data is updated successfully in the database, inside my controller action the update performed by :

$user->update($data);

The part that doesn't work:

I've used boots method updated inside my model like :

class User extends BaseModel
{
    ...

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

        self::updated(function($model){
            Log::info("updated");
            dd($model);
        });
    }
}

The event was never reached I'm not sure why.

Problem: I'm trying to perform an action after the model update but the event doesn't fire.

Upvotes: 5

Views: 2319

Answers (2)

apokryfos
apokryfos

Reputation: 40690

Here's what the manual states with update()

When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.

You need to use save to trigger events. Something like:

$user->fill($data);
$user->save();

This of course is assuming that $user is a model and not a query builder instance.

Upvotes: 7

Jaycee Daily
Jaycee Daily

Reputation: 63

You are accessing the function statically: instead try using

self::updated(function($model){
            Log::info("updated");
            dd($model);
        });

Upvotes: 0

Related Questions