ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[danger] 批量彻底删除 ~~~ $data = User::onlyTrashed()->select($ids); foreach ($data as $v) { $v->force()->delete(); } ~~~ >[danger] 批量恢复数据 ~~~ $data = User::onlyTrashed()->select($ids); foreach ($data as $v) { $v->restore(); } ~~~ >[danger] 模型关联软删除 ``` public function user() { return $this->belongsTo(User::class, 'user_id', 'id')->removeOption('soft_delete'); } ``` >[danger] 常用方法 ``` useSoftDelete('delete_time', time()) ``` >[danger] 关联删除优雅写法 ``` /** * 删除商家 * * @param integer $merch_id 商家id */ public static function delete(int $merch_id) { $merch = ModelMerch::with(['goods'])->findOrEmpty($merch_id); $merch->isEmpty() && fault('商家不存在或已被删除'); // 启动事务 $merch->startTrans(); try { // 删除活动及其中奖者信息 $raffle = $merch->raffle()->with(['winning'])->select(); foreach ($raffle as $v) { $v->together(['winning'])->delete(); } // 删除商家及其商品 $merch->together(['goods'])->delete(); $merch->commit(); } catch (\Exception $e) { $merch->rollback(); fault('删除失败'); } } ```