实际开发中并不会真的删除表数据,而是用一个字段表示该记录已经删除。tp5中默认的该字段是delete_time,
也可以在模型中自己设置,由上面的代码可知自己设置的方法是
protected $deleteTime = '这里是表中软删除字段';
软删除的使用,在模型中use 即可,
namespace app\index\model;
use think\Model;
use traits\model\SoftDelete;
class User extends Model{
use SoftDelete;
}
控制器代码,
namespace app\index\controller;
use think\Controller;
use app\index\model\User;
class Index extends Controller{
public function index(){
//软删除
$res = User::destroy(1);
$res = User::destroy([2,3]);