php - Should we use only narrative methods in controllers when using Repository Pattern in Laravel 5.x?

310

I have a typical repository pattern structure in Laravel. Taking Fruits model as an example I have:

ClassEloquentFruitRepository that extends abstractEloquentRepository that implementsFruitRepositoryInterface.

FruitRepositoryInterface extendsRepositoryInterface.

FinallyRepositoryInterface defines those generic / shared methods likeall,find,with,create etc.

I have read literally everything about Repository Pattern in Laravel 5. I reviewed all GitHub projects and reviewed all gists about that and... I have concern.

I like the approach of using just narrative methods, that in case of my Fruit model should sit inEloquentFruitRepository.

So in my controller instead of building the stuff like:

$fruits = $this->fruitRepository
    ->where('color', '=', 'yellow')
    ->where('is_available', '=', true)
    ->with(['comments'])
    ->orderBy([
        'sweetness' => 'asc'
    ])
    ->all();

is it better just to do

$fruits = $this->fruitRepository
    ->getAvailableYellowFruitsWithPeopleCommentsOrderedBySweetness();

and then define that method inEloquentFruitRepository like:

public function getAvailableYellowFruitsWithPeopleCommentsOrderedBySweetness(): Collection
{
    $model = $this->makeModel();

    $model
        ->where('color', '=', 'yellow')
        ->where('is_available', '=', true)
        ->with(['comments'])
        ->orderBy([
            'sweetness' => 'asc'
        ]);

    return $model->get() ?? new Collection();
}

So in general, should all those generic methods be used only within particular eloquent repositories or is it fine to use them also in controller?

Currently both ways work. I'm asking about best (the very best) practice, not anyone's preference.

Should we only use narrative methods in controllers?

Thanks for any feedback.

747

Answer

Solution:

You're searching for a 'very best' practice but, as you might know, when it comes to Design Patterns there isn't a general 'best practice' that fits all the situations

That being said, a general rule is: when you need a query in more than one point of your application, put it in a Repository. If you need it only once it's up to you

I'd put it in a repository too, but i'd name the method with a shorter and more readable name, i.e:getAvailableYellowFruits. To me the 'with' and 'order by' part is negligible and if you want you can always put that info in the comments of the method

People are also looking for solutions to the problem: wordpress - Woocommerce - How to change a link in php checkout page

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.