longvanren
longvanren

Reputation: 52

How to detect change in specific column in laravel model

I have a table structure like this :

subscribers with attributes: id, status

I want to have a way to check for the column status : If the status change to 'unsubscribed' then i want to auto detect thats change and apply do something with that.

The workflow is like this :

Anywhere in the code call $subscriber->status = 'unsubscribed'; I want to detect that and do something with that.

Thanks for reading.

I had use isDirty of laravel but that had to write after every $subscriber->status = 'unsubscribed'; so its did not do the work.

Upvotes: 1

Views: 1327

Answers (2)

Dz Kien
Dz Kien

Reputation: 1

I believe you can do that

const pagination = computed(() => {
    const item = {} as IPaginationAnt;
    item.total = option.value.pagination.totalElements;
    item.current = option.value.pagination.number;
    item.pageSize = option.value.pagination.size;
    return item;
  });

Upvotes: 0

Jonas
Jonas

Reputation: 379

Add the following code to your model:

protected static function booted()
{
    static::updated(function (Subscriber $subscriber) {
        if ($subscriber->wasChanged('status')) {
            // Execute your code
        }
    });
}

Alternatively, you can use an Observer.

Upvotes: 4

Related Questions