【问题标题】:How can I set future Soft Deleted in laravel如何在 laravel 中设置未来的软删除
【发布时间】:2019-01-03 07:21:25
【问题描述】:

根据这个问题:Is it possible to perform "future" soft delete in laravel? 它可以在 laravel 5.6 中使用吗?如果可以的话我在哪里可以创建特征

我尝试在 app\Traits\ 中创建 Traits,但是当我在模型中调用时它总是说 Trait 'App\Traits\MySoftDeletingTrait' not found

我在 app\Traits 中创建了 2 个文件 - MySoftDeletingScope.php

namespace App\Traits;

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

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

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

the 2nd one MySoftDeleteingTrait.php

namespace App\Traits;

use Illuminate\Database\Eloquent\SoftDeletingTrait;

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

在 Uer php 中我调用

使用 App\Traits\MySoftDeletingTrait; 使用 Notifiable、HasRoles、MySoftDeletingTrait;

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您需要双重指定您使用的特征。首先,在文件开头的类定义之外作为包含:

    use App\Traits\MySoftDeletingTrait;
    

    然后在班级内部:

    use Notifiable, HasRoles, MySoftDeletingTrait;
    

    【讨论】:

    • thnks 我现在可以使用它,但我得到并且错误 withTrashed 不存在。我该如何解决这个问题,我已经添加了 extends Illuminate\Database\Eloquent\SoftDeletingScope
    • 您是否也将要删除的模型设置为使用软删除(根据laravel.com/docs/5.7/eloquent#soft-deleting),例如class User extends Model { use SoftDeletes;
    • 不,我使用 MySoftDeletingTrait 而不是 SoftDeletes,因为我覆盖了一些函数,并且在 MySoftDeletingTrait 中我已经使用 extend extends Illuminate\Database\Eloquent\SoftDeletingScope 这意味着我也可以使用 softdelete 函数不是吗?
    • 数据库迁移是否正确? User 表是否有 deleted_at 列?
    • 是的,我有。 nvm 感谢您的帮助,我会找到 wat 做 nxt
    猜你喜欢
    • 1970-01-01
    • 2015-04-25
    • 2016-09-25
    • 2022-11-02
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    相关资源
    最近更新 更多