Php 模型事件&x27;炮弹没有开火
我的Post模型中有以下代码。创建的Php 模型事件&x27;炮弹没有开火,php,laravel-4,Php,Laravel 4,我的Post模型中有以下代码。创建的事件被触发,但更新的事件未被触发 我正在更新模型,如下所示: Post::where('key','=','some_key_goes_here')->first()->fill(['status' => 'approved'])->save(); public function Post extends \Eloquent { public static function boot() { paren
事件被触发,但更新的事件未被触发
我正在更新模型,如下所示:
Post::where('key','=','some_key_goes_here')->first()->fill(['status' => 'approved'])->save();
public function Post extends \Eloquent {
public static function boot()
{
parent::boot();
Post::created(function ($model) {
Queue::push('\MyApp\Jobs\Notifications\sendNotification', [
'id' => $model->user_id,
'key' => $model->key,
'status' => $model->status
]);
});
Post::updated(function ($model) {
//if the status has changed
$statusChanged = false;
foreach ($model->getDirty() as $attribute => $value) {
if ($attribute == 'status' && $model->getOriginal($attribute) != $value) {
$statusChanged = true;
}
}
if ($statusChanged) {
Notification::add($model->key, $model->status);
}
});
}
}
我的模型是这样的:
Post::where('key','=','some_key_goes_here')->first()->fill(['status' => 'approved'])->save();
public function Post extends \Eloquent {
public static function boot()
{
parent::boot();
Post::created(function ($model) {
Queue::push('\MyApp\Jobs\Notifications\sendNotification', [
'id' => $model->user_id,
'key' => $model->key,
'status' => $model->status
]);
});
Post::updated(function ($model) {
//if the status has changed
$statusChanged = false;
foreach ($model->getDirty() as $attribute => $value) {
if ($attribute == 'status' && $model->getOriginal($attribute) != $value) {
$statusChanged = true;
}
}
if ($statusChanged) {
Notification::add($model->key, $model->status);
}
});
}
}
更新后的+getOriginal第二个参数版本略短一些,作为可能的修复
Post::updated(function ($model) {
//if the status has changed
foreach ($model->getDirty() as $attribute => $value) {
if ($attribute == 'status' && $model->getOriginal($attribute, $value) != $value) {
Notification::add($model->key, $model->status);
break;
}
}
});
你是如何看待这次活动的?