fileIds = $fileIds; $this->fileObjectType = $fileObjectType; $this->files = File::query() ->with(['folder']) ->whereIn("id", $fileIds) ->where("created_by", Auth::id()) ->whereNull("object_id") ->where("object_type", $fileObjectType->value) ->where("created_at", ">=", Carbon::now()->subHours(3)) ->orderBy("created_at") ->get(); foreach ($this->files as $file) { if (! $file->folder_id) { continue; } throw_validation_if($fileUUID != $file->folder->uuid, "illegal uuid"); throw_validation_if($file->folder->object_id, "The folder has been associated and cannot be associated again"); } } public function association(string $objectId,) { if (! $this->files) { return; } foreach ($this->files as $file) { if ($file->source == FileSource::ATTACHMENT->value) { $version = File::query() ->where("created_by", Auth::id()) ->where("object_id", $objectId) ->where("object_type", $this->fileObjectType->value) ->where("title", $file->title) ->where("folder_id", $file->folder_id) ->count(); $file->version = $version + 1; } $file->object_id = $objectId; $file->save(); if ($file->folder) { $file->folder->object_id = $objectId; $file->folder->save(); } } } }