12345678910111213141516171819202122232425262728 |
- <?php
- namespace App\Services\Folder;
- use App\Models\File;
- use App\Models\Folder;
- use App\Models\NamingRule;
- use Illuminate\Support\Facades\DB;
- class FoldersService
- {
- public function applyNaming(string $id, string $namingRuleId): void
- {
- $folder = Folder::query()->findOrFail($id);
- $namingRule = NamingRule::query()->findOrFail($namingRuleId);
- // 子孙均需更新
- DB::transaction(function () use ($folder, $namingRule) {
- Folder::query()
- ->where('path', 'like', $folder->path . '%')
- ->update(['naming_rule_id' => $namingRule->id]);
- File::query()
- ->join('folders', 'files.folder_id', '=', 'folders.id')
- ->where('folders.path', 'like', $folder->path . '%')
- ->update(['files.naming_rule_id' => $namingRule->id]);
- });
- }
- }
|