14 thoughts on “Learn about Using Repositories and Services in Laravel 5

  1. Another “hello world” article…
    What i should todo if there are different validation rules for CREATE and UPDATE actions?
    What if validation rules are different for different user roles?
    What if i need update more than one model while UPDATE action? For ex. Attachment model
    There are already hundreds articles about service layers but there is no article with bit deeper explanation.

    1. It is not necessary you type hint PostRequest in all functions. You can create more custom formrequests as per need of validation. Service layer is not for validation in laravel, but to put logic which concerns after validation. Like index function, we can show posts which are by logged user or all, that logic goes to service layer.

  2. I agree with the above comment. First of all, I understand that this is just a silly example of creating repositories and services, but i think that many beginner programmers can think that this is the right way of creating a module for blogging, which of course it isn’t because when you create a REAL blogging application you would never use a column for storing the content of your post, especially when the column is string typed, in that case you’d better use a text column or something like that.
    Now, going straight to my point, I think most of the real life scenarios include more login than simply storing and retrieving data, I mean, it could be that a software needs to log a lot events before storing a post instance., it might be the case when you’d need to limit some kind of accessible information depending on the role that the current user has.
    I appreciate your help, and this posts but i completely agree with Roman’s comment. There isn’t actually an article that goes deeply with more complicated or REAL scenarios.

    1. same question here (new to laravel), and trying to understand if (and where) to tell Laravel which implementation to choose for the interface I injected in constr of controller

  3. What is the purpose of a Repository like this? I’ve used it too in the past, but i am changing my mind because, as far as i can tell, i only end up rewriting Eloquent..

    1. A repository should be used with an interface too, think of a repository as your data abstraction layer, now imagine the tutorial has a PostRepository interface located in namespace App\Repositories\Contracts and the concrete implementation was defined as Post in the following namespace App\Repositories\Eloquent and would implement your PostRepository interface.

      Within the app service provider, you would then bind the PostRepository interface to the Post repository class and in the Service class, inject an instance of the PostRepository interface.

      Now imagine many months have passed and for some reason it was decided that the project owner no longer wished to use Eloquent, they now want to use Mongo.

      In response, you would create a new post repository class in namespace App\Repositories\Mongo using the same interface, but within its methods, write specific code to access the data from Mongo. Then in the app service provider, change the binding to use the new repository class and suddenly without touching the service layer or your controller, your application is using the new data source.

      The point is, your service layer would not care about the data source, it only cares about the contract the interface promises your data layer will fulfil.

Leave a Reply

Your email address will not be published. Required fields are marked *