data, 'files'); $filesCreated = []; foreach ($files as $file) { //save each file $filePayload = new CreateFileDTO(transmittal_id: $payload->transmittal->id, description: Arr::first($file)['description']); $fileCreated = $this->storeFileCommand->execute($filePayload->except('file')->toArray()); $comments = Arr::first($file)['comments']; $remarks = Arr::first($file)['remarks']; //save each comments of each file foreach ($comments as $key => $comment) { $commentPayload = new CreateCommentDTO(file_id: $fileCreated->id, comment: $comment['comment']); $this->storeCommentCommand->execute($commentPayload->except('commentModel')->toArray()); } //save each remarks of each file foreach ($remarks as $key => $remark) { $remarkPayload = new CreateRemarkDTO(file_id: $fileCreated->id, remark: $remark['remark']); $this->storeRemarkCommand->execute($remarkPayload->except('remarkModel')->toArray()); } $filesCreated[] = $fileCreated; } $payload->files = $filesCreated; return $next($payload); } catch (Exception $exception) { throw new LogicException('Error creating transmittal files: ' . $exception->getMessage()); } } }