简体   繁体   中英

Laravel: How to fire event when a particular column in users table changes?

In my users table I have a column with the name verified and data type boolean with default value 'false', However, I want to fire an event when that column value to any particular user changes to 'true', So i can make a notification based on that event? Any ideas how I can achieve this?

You could add a hook to the model event and perform a check. The code below goes in your model. You may or may not want to pass the user object with the event. This might come in handy if you're sending notifications.

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

    static::updated(function($user) {
        if ($user->verified == true) {
            event(new UserVerified($user));
        }
    });
}

What I am thinking of is to create a trigger on update of your target table. Upon update, you can insert a record into another table EVENT .

Your code can be periodically polling from that the EVENT table to process these updates.

If you are using Oracle DB, Oracle queues will be very useful.

You could use the Attribute Events package to fire events when a model attribute changes: https://github.com/jpkleemans/attribute-events

class User extends Model
{
    protected $dispatchesEvents = [
        'verified:true' => UserVerified::class,
    ];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM