Ad

Logic Of Repository Pattern Laravel

I am learning to use the Laravel framework.

I came across the repositories, said in several sites be a good practice of development.

I found some packages that help implement this practice in Laravel, but missed some features, but I think I'm failing to understand very well the logic and its benefits.

Considering studying that part, I created a package that shows how to think is this implementation, but I still have many questions.

https://github.com/guilhermegonzaga/repository

Ex:

Scopes and relations should be transferred to the repository or kept in the model?

How to implement the type functions "increment(), decrement()" in the repository? or is it correct to call them on the controller?

$model = $repository->find($id);
$model->increment();

Thank you

Ad

Answer

You should have the relationships set in the model:

https://laravel.com/docs/5.1/eloquent-relationships

When you are talking to the model from within the repo you can then pull the relationship information if needed. The increment and decrement should be done in the repository.

Check out this quick article to get a better understating of what you are trying to achieve using the repository pattern:

http://culttt.com/2014/09/08/benefits-using-repositories/

Ad
source: stackoverflow.com
Ad