DEV Community

Cover image for Implement CRUD with Laravel Service-Repository Pattern

Implement CRUD with Laravel Service-Repository Pattern

Saf11000 on May 25, 2020

Why Laravel Service and Repository Pattern? Yes, indeed, there are many ways that laravel can interpret the CRUD functionality. But I personal...
Collapse
 
ostap profile image
Ostap Brehin • Edited

Why create these complications? (additional layers on top of simple Eloquent CRUD)

I don't think it's good practice in that case.

adelf.tech/2019/useless-eloquent-r...

Collapse
 
safventure11000 profile image
Saf11000 • Edited

Hi,

Thanks for dropping by.

Based on my experience, Laravel service-repository pattern is conducive if you're working in a big team or big projects. It is also sustainable and easy to understand to separate the logic and the wrapper of the model. The concept of repositories and services ensures that you write reusable code and helps to keep your controller as simple as possible making them more readable.

Collapse
 
nelson1212 profile image
NELSON LÓPEZ

I agree with you that the Laravel service-repository pattern is recommended for large teams or big projects.

Collapse
 
jamols09 profile image
Info Comment hidden by post author - thread only accessible via permalink
jamols09

Hello

This guide is really great and I am trying to follow your guideline because I found tutorials from youtube pretty hard it this one is much simplier. I would just like to ask if you're kindly able to check my repo and do some recommendations/comments since I'm really not sure where to put the logic for pagination because it has some search filters and other types. Thank you very much!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
jamols09 profile image
jamols09 • Edited

Hey sorry for bugging but I was wondering when following this pattern where should I put my logic for search filter because I wrote it on the repository but I'm not sure if this is the way to go.
github.com/jamols09/doc-backend/bl...

Thread Thread
 
safventure11000 profile image
Saf11000

sent you a pm

Collapse
 
jamols09 profile image
jamols09

Thank you very much. Whenever your time pleases repo: github.com/jamols09/doc-backend

Collapse
 
mahdipishguy profile image
Mahdi Pishguy

i think service-repository implementation difficult than repository-pattern, do you agree?

Collapse
 
safventure11000 profile image
Saf11000 • Edited

I agree with you. I think laravel repository pattern implementation is easier because you only need to add the repository. But if you're thinking on the readability and flexibility of your system, I highly recommend the service-repository pattern. Separating the logic (service) and the wrapper of the model (repository) is not hard at all.

Collapse
 
mahdipishguy profile image
Mahdi Pishguy

I will definitely test it

Collapse
 
bluewater0506 profile image
ilya

your laravel repository is good.
Do you know about Transformer of laravel repository?
Thank you

Collapse
 
safventure11000 profile image
Saf11000 • Edited

Thanks. No... but maybe this will help you
andersonandra.de/l5-repository/

Collapse
 
bluewater0506 profile image
ilya

soory but i am using laravel8.
if you know about repository of laravel 8, please teach me.
Thank you

Thread Thread
 
safventure11000 profile image
Saf11000

maybe my sample project can help you. - github.com/jsafe00/her-running-med...

repository pattern with laravel 8 and php 8...

Thread Thread
 
bluewater0506 profile image
ilya

Hello.
Thank you very much.
Sorry but i want to communicate in chat.
You are talent.
This is my skype id:
live:.cid.55d5d2aa7799e08d
Please contact.....

Collapse
 
bluewater0506 profile image
ilya

Thank you
I am following you and want to learn from you.
good regards.

Collapse
 
lokesh7186 profile image
Lokesh Tulsani

I think the service layer is enough for the CRUD operation here and Repository is not at all required since you’re only abstracting Eloquent queries inside the repository.

Also a repository is nothing more than an adapter for a specific persistence and it should not implement any business logic nor application logic.

Collapse
 
safventure11000 profile image
Saf11000

Hi,

Thanks for sharing your input. For the purpose of showing the flow of the pattern, I’m using a simple CRUD. Separating the wrapper of the model and business logic is useful if you’re working in a big team or big projects.

Collapse
 
davidjije profile image
David Jijelava

Hi.
Thank you for this clear and great post.
I was wondering about this pattern and your instructions are so helpful.
But there is one thing. I am wondering how can i write unit tests in this pattern?
Should I test Repository methods? or Service methods? Which and how?

Collapse
 
safventure11000 profile image
Saf11000

Sorry, I haven't tried that.

Collapse
 
pablocamara profile image
Pablo Câmara

Thanks for this post it definitely explains things nicely and I learned from it!

Collapse
 
safventure11000 profile image
Saf11000 • Edited

Happy to hear you find this helpful. :)

Collapse
 
bluewater0506 profile image
ilya

Hello
I am Ilya
Laravel has not repository command.
This is correct?

Collapse
 
safventure11000 profile image
Saf11000

Yes. As of now, there is no artisan command for repository or service. But there are composer packages that you can install that will generate these.

Collapse
 
bluewater0506 profile image
ilya

Thank you.
then, Could you please send me composer install command for repository?
good regards

Thread Thread
 
safventure11000 profile image
Saf11000 • Edited

Not sure if these will work because it always depends on your laravel version. That's why I prefer to just create it manually.

github.com/OzanKurt/Repoist - composer require ozankurt/repoist
github.com/jason-guru/laravel-make... - composer require jason-guru/laravel-make-repository --dev

Collapse
 
jaihari profile image
harish vagjiyani

Thanks

Collapse
 
sosaheri profile image
Heriberto Sosa

I will try it

Collapse
 
sdwru profile image
sdwru

Another way to do validation is by injecting request class into controller.

laravel.com/docs/8.x/validation#cr...

Collapse
 
safventure11000 profile image
Saf11000

thanks for sharing this input.

Collapse
 
sagarsun21 profile image
sagar-sun21

Can you please suggest me how can I do dependency injection?

Collapse
 
safventure11000 profile image
Saf11000

I read an article here about laravel dependency injection. This might be useful for you. dev.to/carlomigueldy/getting-start...

Collapse
 
nelson1212 profile image
NELSON LÓPEZ

Thanks. I appreciate your post.

Some comments have been hidden by the post's author - find out more