dsideal2015 2019-01-03 07:21
浏览 33
已采纳

如何在laravel中设置未来的Soft Deleted

According to this Question : Is it possible to perform "future" soft delete in laravel? Is it can use in laravel 5.6 ? and If It can Where can I create Traits

I try to create Traits in app\Traits\ but when I call in in models It always said Trait 'App\Traits\MySoftDeletingTrait' not found

I created 2 file in app\Traits - 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);
    }
}

In Uer php I call

use App\Traits\MySoftDeletingTrait; use Notifiable, HasRoles, MySoftDeletingTrait;

  • 写回答

1条回答 默认 最新

  • dongyu2047 2019-01-03 07:41
    关注

    You need to doubly specify the traits that you use. Firstly, outside of the class definition at the start of the file as an include:

    use App\Traits\MySoftDeletingTrait;
    

    and then later on inside of the class:

    use Notifiable, HasRoles, MySoftDeletingTrait;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探