美文网首页
TP5.0的软删除实现

TP5.0的软删除实现

作者: IT行者q | 来源:发表于2019-03-19 16:58 被阅读0次

1第一步:
在数据表中添加delete_time字段,默认为null。

2第二步:
在模型中引入traits\model\SoftDelete 类,设置软删除的标志 protected $deleteTime = 'delete_time';
注意:5.0.2版本之前 deleteTime 属性必须使用 static 定义。
代码如:

      namespace app\index\model;
      use think\Model;
      use traits\model\SoftDelete;

      class User extends Model
      {
          use SoftDelete;
          protected $deleteTime = 'delete_time';
      }

3.第三步:
使用destory()和delete()方法后,再用find()和select()方法查询数据时,就不会再查找软删除后的数据。
如果想查找出软删除后的数据 就用 withTrashed() 方法。

          User::withTrashed()->find();
          User::withTrashed()->select();

相关文章

网友评论

      本文标题:TP5.0的软删除实现

      本文链接:https://www.haomeiwen.com/subject/ugpgmqtx.html