Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use models vs service in Laravel?

I have been using for a while but still I am confuse on what to include in my controllers specifically "When should I use Model and When to Use Service"?

I am confused if I am making the right thing.

like image 523
John Roca Avatar asked Mar 11 '16 00:03

John Roca


2 Answers

I would suggest to never use models directly into the controllers.. Adopt the repository-pattern which enables the use of dependency injection of services into the controller.. Use Service Layer to contain the business logic.. In this way all the related codes would be onto similar layer.

Reference for repository pattern: http://heera.it/laravel-repository-pattern#.VuJcVfl97cs

like image 156
dpak005 Avatar answered Sep 23 '22 01:09

dpak005


Please don't use model directly in your controller, unless if you are building a very simple application but for a large application, it would be good to use a repository. Kindly take a look at this tutorial https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/

like image 36
Nonong Re Avatar answered Sep 19 '22 01:09

Nonong Re