模型事件不起作用

时间:2014-05-22 19:04:28

标签: events laravel model laravel-4

我有以下型号:

class ProjectTwitterStatus extends Eloquent {

    protected $table = 'project_twitter_statuses';

    protected $softDelete = true;

    protected $guarded = array('id');

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

        ProjectTwitterStatus::deleting(function($projectTwitterStatus)
        {
            $projectTwitterStatus->deleted_by = Auth::user()->id;
        });
    }

    public function twitterStatus() {
        return $this->belongsTo('TwitterStatus');
    }

    public function twitterRetweet() {
        return $this->belongsTo('TwitterRetweet','twitter_status_id','twitter_status_id');
    }

    public function project() {
        return $this->belongsTo('Project');
    }
}

在我的应用程序的某处,使用以下语句之一删除项目:

ProjectTwitterStatus::find($id)->delete();
ProjectTwitterStatus::whereIn('twitter_status_id', $twitterStatusIds)->delete();

我可以在数据库中看到该项目已被(软)删除。但是,未填充deleted_by列。

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试使用Late Static Binding,如下所示 -

class ProjectTwitterStatus extends Eloquent {

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

        static::deleting(function($projectTwitterStatus)
        {
            $projectTwitterStatus->deleted_by = Auth::user()->id;
        });
    }

}