仅在更新模型 properties/columns 时处理事件 - Laravel 观察者
Handling events only when model properties/columns are updated - Laravel Observer
我在这里处理一个模型的两个事件,如您在下面的代码中所见,更新和更新。我担心的是,只有当 teacherId 已更改时,我才想在更新事件中执行一些任务,所以我想检查更新事件中的值并使用 class 属性 来知道它是否是否已更改,但该标志始终 returns 假假定其定义值。
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");
}
}
这是正确的方法吗?如果不是我做错了什么?
在 Eloquent 模型中,您可以在更新属性时使用 getOriginal
方法。
所以如果你想获得 teacherId
的原始值(更新前),你可以这样做:
$subject->getOriginal('teacher_id');
见https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html#method_getOriginal
在您的代码中:
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');
}
我在这里处理一个模型的两个事件,如您在下面的代码中所见,更新和更新。我担心的是,只有当 teacherId 已更改时,我才想在更新事件中执行一些任务,所以我想检查更新事件中的值并使用 class 属性 来知道它是否是否已更改,但该标志始终 returns 假假定其定义值。
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");
}
}
这是正确的方法吗?如果不是我做错了什么?
在 Eloquent 模型中,您可以在更新属性时使用 getOriginal
方法。
所以如果你想获得 teacherId
的原始值(更新前),你可以这样做:
$subject->getOriginal('teacher_id');
见https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html#method_getOriginal
在您的代码中:
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');
}