Yes you can, and the pipeline pattern would do the job perfectly, and keep your code organized for future scaling, if the input is empty, then it will be ignored in the query, the idea is moving your query through pipes, and in each pipe you decide what to do, either add something or remove something based on a condition, in your case if that input is set or no.
This article will get you started: https://dev.to/abrardev99/pipeline-pattern-in-laravel-278p
And actually the code is close to what you need I guess, it will be slight changes.