摘要:<?php namespace app\index\controller; use think\Controller; use think\Db; use app\index\model\Users as UsersModel; class Users extends Controller { //
<?php namespace app\index\controller; use think\Controller; use think\Db; use app\index\model\Users as UsersModel; class Users extends Controller { // 使用模型刪除 destroy(); public function del() { // 使用閉包刪除 UsersModel::destroy(function($query){ $query->where('weight','>',130);// 體重大于130的刪除 }); } /** * 實現(xiàn)軟刪除的步驟: * 1、在數(shù)據(jù)庫表中(users) 添加字段:delete_time; * 2、在Users模型中引入SoftDelete類庫;think\model\concern\SoftDelete; * 3、在Users模型中添加兩個屬性 * $deleteTime = 'delete_time';// 與數(shù)據(jù)表字段名一致 * $defaultSoftDelete = 0;// 添加刪除標志默認值; * 4、在控制器中使用withTrashed() 方法實現(xiàn)軟刪除 * 5、查看已刪除的軟刪除;onlyTrashed(); * 6、恢復軟刪除 (new UsersModel)->restore(條件); */ // 使用模型實現(xiàn)軟刪除 public function softdelete() { // 如果想在查詢的時候看到已經(jīng)被刪除的記錄 使用:withTrashed(); $res = UsersModel::withTrashed()->where('uid','<',5)->select(); dump($res); } // 恢復軟刪除 使用依賴注入方式恢復 public function restore(UsersModel $users) { // 恢復軟刪除 $res = $users->restore(['uid'=>1]); dump($res); } } ?>
批改老師:天蓬老師批改時間:2019-04-22 10:19:30
老師總結:如果你是軟刪除, 那么在查詢的時候, 一定要注意一下查詢條件... 是否允許過濾掉已軟刪除的記錄, ....