FoldersService.php 877 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. namespace App\Services\Folder;
  3. use App\Models\File;
  4. use App\Models\Folder;
  5. use App\Models\NamingRule;
  6. use Illuminate\Support\Facades\DB;
  7. class FoldersService
  8. {
  9. public function applyNaming(string $id, string $namingRuleId): void
  10. {
  11. $folder = Folder::query()->findOrFail($id);
  12. $namingRule = NamingRule::query()->findOrFail($namingRuleId);
  13. // 子孙均需更新
  14. DB::transaction(function () use ($folder, $namingRule) {
  15. Folder::query()
  16. ->where('path', 'like', $folder->path . '%')
  17. ->update(['naming_rule_id' => $namingRule->id]);
  18. File::query()
  19. ->join('folders', 'files.folder_id', '=', 'folders.id')
  20. ->where('folders.path', 'like', $folder->path . '%')
  21. ->update(['files.naming_rule_id' => $namingRule->id]);
  22. });
  23. }
  24. }