08-06-2023, 03:44 PM
I am currently creating an API using [lumen][1] and I'm not entirely confident about where I should be putting my database queries. I am using the Repository pattern and currently my layout is like so:
1. Controller loads Custom Repository
2. Repository method contains eloquent query and returns result.
`Controller --> Custom Repository --> Model`
`Controller <-- Custom Repository <-- Model`
--
A high level code example of how I am currently doing it:
Controller.php
public function browse()
{
// customRepo added via dependency injection
$this->customRepo->browse()
}
customRepo.php
public function browse()
{
// other logic here
return Member::where('active', 1)->orderBy('date', 'desc')->get()
}
I am using Eloquent to make queries to the database and am currently all of these calls are happening inside the Repository which feels a bit odd to me because my Repositories are filling up with Eloquent (and some query builder) queries however I have seen from a few sources that it's [not correct][2] to put the queries inside the model.
I feel like my current approach is probably correct I just wanted to see if anyone could definitively tell me which is best - it doesn't make a whole lot of sense for me to pad out the models with custom methods if not needed.
[1]:
1. Controller loads Custom Repository
2. Repository method contains eloquent query and returns result.
`Controller --> Custom Repository --> Model`
`Controller <-- Custom Repository <-- Model`
--
A high level code example of how I am currently doing it:
Controller.php
public function browse()
{
// customRepo added via dependency injection
$this->customRepo->browse()
}
customRepo.php
public function browse()
{
// other logic here
return Member::where('active', 1)->orderBy('date', 'desc')->get()
}
I am using Eloquent to make queries to the database and am currently all of these calls are happening inside the Repository which feels a bit odd to me because my Repositories are filling up with Eloquent (and some query builder) queries however I have seen from a few sources that it's [not correct][2] to put the queries inside the model.
I feel like my current approach is probably correct I just wanted to see if anyone could definitively tell me which is best - it doesn't make a whole lot of sense for me to pad out the models with custom methods if not needed.
[1]:
[To see links please register here]
[2]:[To see links please register here]