ホームページ >バックエンド開発 >PHPチュートリアル >Laravel'の強制的な削除
メソッドは、永久に削除する前にモデルを取得することなく、このプロセスを簡素化します。 forceDestroy
次のことは、
メソッドの使用方法の例です。
forceDestroy
use App\Models\Post; // 永久删除单个记录 Post::forceDestroy($id); // 删除多条记录 Post::forceDestroy([$id1, $id2, $id3]);
<?php namespace App\Services; use App\Models\User; use App\Models\Content; use Illuminate\Support\Facades\Log; use App\Events\UserDataPurged; class DataCleanupService { public function purgeInactiveUserData(int $monthsInactive = 12) { $inactiveUsers = User::onlyTrashed() ->where('deleted_at', '<', now()->subMonths($monthsInactive)) ->pluck('id'); if ($inactiveUsers->isEmpty()) { return ['message' => '没有需要清理的非活动用户']; } // 首先清理相关内容 $contentCount = Content::onlyTrashed() ->whereIn('user_id', $inactiveUsers) ->count(); Content::whereIn('user_id', $inactiveUsers) ->forceDestroy(); // 永久删除用户帐户 $userCount = User::forceDestroy($inactiveUsers); Log::info('完成用户数据清理', [ '已删除用户数量' => $userCount, '已删除内容数量' => $contentCount ]); UserDataPurged::dispatch($inactiveUsers); return [ '已清理用户数量' => $userCount, '已清理内容数量' => $contentCount, 'message' => "已成功清理 {$userCount} 个非活动用户帐户" ]; } }メソッドは、永続的な削除操作を簡素化し、ソフト削除レコードを管理するときにコードをより簡潔で効率的にします。
以上がLaravel&#039;の強制的な削除の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。