I’m working on a Laravel application where I have a UserDTO representing user data, including attributes like first_name, last_name, and url. I’m using a FormRequest to validate and handle user input before passing it to the DTO.
Now, I need to handle uploaded files (e.g., user avatars) in this workflow. I’m wondering what the best practice is for dealing with uploaded files in Laravel DTOs and FormRequests.
Should I process the file separately before passing the URL to the DTO or FormRequest? If so, what’s the recommended approach for processing and storing the file and generating the URL?
Should I modify my DTO to accept file instances or data in addition to other attributes? How can I handle file processing within the DTO without violating the single responsibility principle?
Can I leverage Laravel’s FormRequest for file handling, such as validation and processing before reaching the DTO or controller? If yes, what’s the recommended way to handle file-related logic in FormRequests?
In Laravel, when working with uploaded files in DTOs (Data Transfer Objects) and FormRequests, I’ve tried the following approaches:
Processing Files Separately: I handled file uploads in the controller or service layer, processed the files (e.g., stored them in storage, generated URLs), and then passed the URLs to the DTO or FormRequest.