是否有可能执行“未来"计划?在laravel中进行软删除? [英] Is it possible to perform "future" soft delete in laravel?

查看:103
本文介绍了是否有可能执行“未来"计划?在laravel中进行软删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现laravel Eloquent ORM中的软删除只是用一个时间戳替换了delete_at列中的null.使用软删除查询表时,是否只是在检查Deleted_at是否为null,或者它是否正在将值与当前时间进行比较?

I found that the soft-delete in laravel Eloquent ORM is just replacing the null in deleted_at column by a timestamp. When querying a table with soft delete, is it just checking if the deleted_at is null, or it is really comparing the value with current time?

我要问是否可以通过在Deleted_at列上设置将来的时间来进行计划删除.

I am asking to see if I am able to do schedule delete by setting a future time on the deleted_at column.

推荐答案

Laravel仅检查deleted_at是否不是NULL. SoftDeletingScope:

Laravel only checks if deleted_at is not NULL. SoftDeletingScope:

public function apply(Builder $builder)
{
    $model = $builder->getModel();

    $builder->whereNull($model->getQualifiedDeletedAtColumn());

    $this->extend($builder);
}


您可以通过创建自己的SoftDeletingScopeSoftDeletingTrait(在Laravel 5中称为SoftDeletes)来更改此设置.


You can change that by creating your own SoftDeletingScope and SoftDeletingTrait (it's called SoftDeletes in Laravel 5).

trait MySoftDeletingTrait {
    use Illuminate\Database\Eloquent\SoftDeletingTrait;

    public static function bootSoftDeletingTrait()
    {
        static::addGlobalScope(new MySoftDeletingScope);
    }
}

还有

class MySoftDeletingScope extends Illuminate\Database\Eloquent\SoftDeletingScope {
    public function apply(Builder $builder)
    {
        $model = $builder->getModel();

        $builder->where($model->getQualifiedDeletedAtColumn(), '<=', Carbon::now());

        $this->extend($builder);
    }
}

注意,要删除作用域(remove()方法),您将必须覆盖更多原始作用域类.至少还有isSoftDeleteConstraint,但我会留给您.

Note that to be able to remove the scope (the remove() method) you would have to override more of the original scope class. At least also isSoftDeleteConstraint, but I'll leave that to you.

最后,您只需要切换模型中使用的特征:

Finally you only have to switch out the trait that you use in your models:

use MySoftDeletingTrait;

这篇关于是否有可能执行“未来"计划?在laravel中进行软删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆