ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1.删除当前模型 1)删除模型数据,实例化后调用delete方法 $user = User::get(1); $user->delete(); 2.根据主键删除 1)直接调用静态方法 User::destroy(1); //批量删除 User::destroy('1,2,3'); User::destroy([4,5,6]); 3.条件删除 1)使用数组进行条件删除 //删除状态为0的数据 User::destroy(['status'=>0]); 2)使用闭包删除 User::destroy(function($query){ $query->where('id','>',10); }); 3)通过数据库类的查询条件删除 User::where('id','>',10)->delete(); ## 软删除 1.需要引入SoftDelete namespace app\index\model; use \think\Model; use \think\SoftDelete; class User extends Model{ use SoftDelete; //数据库中对应的字段名称 protected $deleteTime = 'delete_time'; } //软删除 User::destroy(1); //真实删除 User::destroy(1,true); $user = User::get(1); //软删除 $user->delete(); //真实删除 $user->delete(true); //查询包含软删除的数据 User::withTrashed()->find(); User::withTrashed()->select(); //只查询软删除的数据 User::onlyTrashed()->find(); User:;onlyTrashed()->select();