laravelmodeleloquentobserverslaravel-events

Handling events only when model properties/columns are updated - Laravel Observer


I am dealing here with two events of a model, updating and updated as you can see in the below code. My concern is that I want to do some task in the updated event only if the teacherId has been changed, So I figured to check the value at the updating event and use a class property to know if it had been changed or not but that flag always returns false assuming its defined value.

namespace App\Observers;

class SubjectObserver {

private $shouldUpdateThreads = false;

/**
 * Listen to the ThreadList created event.
 * This events Fire when ThreadList is handled
 * @param \App\Observers\App\ThreadList $threadList
 */
public function created(\subject $subject) {

}

/**
 * Listen to the ThreadList saved event.
 * @param \App\Observers\App\ThreadList $threadList
 */
public function saved(\subject $subject) {

}

/**
 * Handling subject updated event 
 * Used to update the threads and related models
 * @param \subject $subject
 */
public function updated(\subject $subject) {
    info('After Update Event ' . $this->shouldUpdateThreads);
    if ($this->shouldUpdateThreads) {
        info_plus($subject);
    }
    info('After Update Check');
}

/**
 * Handling subject being updated event
 * Used to check if the teachers data has changed or not
 * @param \subject $subject
 */
public function updating(\subject $_subject) {

    $subject = \subject::find($_subject->id);

    $this->shouldUpdateThreads = ($subject->teacherId != $_subject->teacherId) ? true : false;

    info(($subject->teacherId != $_subject->teacherId));

    info("Thread update ? " . $this->shouldUpdateThreads);
}

public function deleted(\subject $subject) {
    info("Subject deleted");
}

}

Is this even the right apporach ? if not what am I doing wrong ?


Solution

  • In Eloquent Models you can use the getOriginal method when updating attributes. So if you want to get the original value (before update) of teacherId you can do:

    $subject->getOriginal('teacher_id');
    

    See https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html#method_getOriginal

    In your code:

    public function updated(\subject $subject) {
        info('After Update Event ' . $this->shouldUpdateThreads);
        if ($subject->getOriginal('teacher_id') !== $subject->teacher_id) {
            info_plus($subject);
        }
        info('After Update Check');
    }