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

lirone's avatar

Where to store some functions ? In the models or inside a dedicated repository ?

I have used now Laravel for a couple of weeks and I have a doubt about something.

I use the repository pattern for accessing data in the DB so that I have an extra layer. Mainly the concerned functions are "store", "all", "get", etc. However, sometimes when I create a new function, I'm asking myself where should I put this function : inside the model or inside the dedicated repository ?

For example : let's say I want to manage devices. Then, I have a file for the model, another for the Controller and eventually a repository called DeviceRepository.

In my repository, I put all the functions that is managing the CRUD part (All, Get, store...).

Now, let's say I want to retrieve all the records (a table that have a device foreign key) that are associated to the device. Let's call this function getRecords().

For me, the most intuitive way to do that is to put this function inside the model Device. So that, I can do myDevice->getRecords() However, I still wonder if I should instead put this function inside the dedicated repository. For example, I would write instead myDeviceRep->getRecords($deviceId)

What do you think ? What are the best practices ? What do you recommend to me ?

Thank a lot

0 likes
3 replies
bugsysha's avatar

@sinnbeck I think that question was just where to encapsulate logic. One is a model and the other is a repository.

What you've posted is a bit long, so I didn't read the whole article, but that does not answer the question, right? I use that same pattern (it is not an action, but command pattern), and it nicely complements that approach Adam is talking about in video I've posted.

Please or to participate in this conversation.