简体   繁体   中英

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM