首先在模型中我们要引入
use traits\Model\SoftDelete;
在类中引入
use SoftDelete;
模型层的代码参考:在这里我们需要注意一个问题,(添加的$delete_time的类型不能是时间戳类型 我们需要手动将其设置成int类型)
<?php
namespace app\admin\model;
use think\Model;
use traits\Model\SoftDelete;
class users extends Model
{
use SoftDelete;
protected $table='users';
protected $deleteTime='delete_time';
public static function del($id)
{
return self::destroy($id); // TODO: Change the autogenerated stub
}
}
首先我们查询出被软删除的数据,将其展示在view上
model 代码参考如下:
public static function seek(){
return self::onlyTrashed()->select();
}
在控制器中恢复被软删除的数据,通过id找到我们要恢复的数据 controller 代码参考如下:
public function recover($id){
$user=new users();
$res=$user->restore(['id'=>$id]);
if(!$res){
$this->error('恢复失败','/admin/show/show');
}else{
$this->success('恢复成功','/admin/show/show');
}
}