Search code examples
phplaravellaravel-5.3

Use custom soft deleted column in laravel


I know that How can I use Soft Deleting feature for models in laravel. like this :

class Flight extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];
}

But I want to use a custom column named sender_deleted_at for that feature where all related methods like forceDelete , restore , withTrashed and etc work based that column.

I wrote this Question But I could not get the right answer.

I'm using Laravel 5.3.


Solution

  • The SoftDeletes trait uses this code to "delete" a row:

    protected function runSoftDelete() {
            $query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());
            $this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp();
            $query->update([$this->getDeletedAtColumn() => $this->fromDateTime($time)]);
    }
    

    The body of getDeletedAtColumn() is:

    public function getDeletedAtColumn() {
        return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
    }
    

    Therefore you can do this:

    class Flight extends Model
    {
        use SoftDeletes;    
        protected $dates = ['my_deleted_at'];
        const DELETED_AT = 'my_deleted_at';
    }