Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

umefarooq's avatar

Where to keep common use queries and code?

Hi, Where should i keep code or db query which is reusable between different controllers. I want to use same query to show report as html view, export pdf and excel files out of it. if any change in future so i will update only in one place. query is joined multiple tables and models.

Same is for code. I am using same code to calculate some rewards and code has calculation formula where should i keep that also to change or update it from one place.

Need some suggestion from community.

0 likes
3 replies
martinbean's avatar

@umefarooq If you need to re-use code, then the solution is to abstract it. There are various ways you could do this.

  • You could create a “repository” class that houses frequently used methods
  • You could create “action” classes, that are single classes that do something.
  • Similarly, you could create dedicated “query” classes, which are classes that literally execute a query and return the results.
umefarooq's avatar

@martinbean Thanks for the reply where we will keep these classes, do we need create separate directory manually under app directory or we can do this with artisan command? and example will be really helpful.

martinbean's avatar

@umefarooq You can store them where ever you want to. If you want to create a separate directory, you’re free to do so.

Please or to participate in this conversation.