Martijn Thomas
Martijn Thomas

Reputation: 951

Model Event not working

I have the following model:

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');
    }
}

Somewhere in my app an item is deleted using one of the following statements:

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

I can see in the database the item had been (soft) deleted. But the deleted_by column is not filled.

Does anyone know what I am doing wrong?

Upvotes: 0

Views: 879

Answers (1)

Rifat
Rifat

Reputation: 7728

Try using Late Static Binding, like following-

class ProjectTwitterStatus extends Eloquent {

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

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

}

Upvotes: 1

Related Questions