Implement CRUD with Laravel Service-Repository Pattern
Tags: laravel, service, repository, patterns
Published: 2020-05-25 13:35:50

<a href="https://www.buymeacoffee.com/jsafe00" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-green.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a>

<b> Why Laravel Service and Repository Pattern? </b>

Yes, indeed, there are many ways that laravel can interpret the CRUD functionality. But I personally suggest the service-repository design pattern because it’s clean and sustainable. 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.

Repositories are usually a common wrapper for your model and the place where you would write different queries in your database. A service on the other hand is a layer for handling all your application’s logic. Based on experience, it’s really conducive to separate the logic and the wrapper of the model especially when you’re working on team or big projects.

To illustrate the repository and service, we’ll build a CRUD application.
I assume you already have laravel project installed in your local machine. If none, you may read my previous post on installing laravel project at https://dev.to/jsafe00/set-up-laravel-project-in-a-virtual-machine-with-laravel-homestead-3d4a.

For the purpose of this tutorial to emphasize the service-repository pattern, I’ll be illustrating the backend side only. You may use postman to execute. 
You can download then install postman here: https://www.postman.com/downloads/

To get started,  let’s set up Model, Controller and Migration by executing: 

<mark> php artisan make:model Post -mcr </mark>

-m, --migration Create a new migration file for the model.
-c, --controller Create a new controller for the model.
-r, --resource Indicates if the generated controller should be a resource controller

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a29x5t08hmba3s3j7vgq.png)

<b><u> Route </u></b>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4vyihgpnb0q9bqz5f0m8.PNG)

<b><u> Model </u></b>
Please ensure that our attributes are fillable. Update Post model like below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l0yuinzyydhciuv92dgn.PNG)

<b><u> Migration </u></b>
Then, update the post migration like below then execute 

<mark> php artisan migrate </mark>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d8n96ete3xx6a0dpcb9e.PNG)

<b><u> Repository </u></b>
Laravel does not have a repository command. You have to do it manually. Just create a Repositories folder then add PostRepository file and add the code below. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nxo3dati9zk77q367p8g.png)

We call the Post model in the constructor of our class.

<b><u> Service </u></b>
Like repository, laravel doesn’t have a service command. Create a Services folder, add PostService file and add the code below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0a4eol5ilb6ytgfyqvby.PNG)

We inject the PostRepository dependency into the constructor of our PostService class.

Now that we are done with the repository-service setup. Let’s proceed with creating our CRUD.

<b><u> Create </u></b>

PostController -> PostService -> PostRepository

https://dev-to-uploads.s3.amazonaws.com/i/7hry719tcwbt0l0zxs4p.png
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qvi4u98hfv4wb6qpv5ls.png)

$this->postService->savePostData($data) – this part calls the savePostData function in the post service.

In the post service, we validate the data. If there are no errors,

$this->postRepository->save($data); - we call the save function in the post repository to save the data in the database.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p9qjo84eo7zgdp3vihs0.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d0kwjkylw0i7wf6yif53.PNG)

If there are errors, for example when we didn’t input a title, then this will be displayed when we execute in postman.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k5lkv6em3tv5s3kkpjmu.PNG)

<b><u> READ </u></b>

<b> GetAllData </b>

https://dev-to-uploads.s3.amazonaws.com/i/zdpkcz7d7pl4kah4pvli.png
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uq13tzlt36mdrq9gezgd.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yshwvzi36lbqvf2kld6p.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rqj1992ghfql3mhv4ijn.PNG)

<b> GetById </b>

https://dev-to-uploads.s3.amazonaws.com/i/old8umw7apwjkvkwqb58.png
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2afuwtmoy2tui7qqsxfp.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ojb1utpbd3ocvd68p6if.PNG)

<b><u> Update </u></b>

https://dev-to-uploads.s3.amazonaws.com/i/uddzrcqmolhupsssrk3c.png
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bkaiwff91w1rc0f2k2dc.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/li7zk5yfszkk0xvebajq.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2c6dhdeajtfoh7xeh7fb.PNG)

<b><u> Delete </u></b>

https://dev-to-uploads.s3.amazonaws.com/i/izq5dwwy2n3fy3mmy4zk.png
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/soug59ml23sgbiwk8euh.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ltzcheow0sibqkw8i1n0.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9i3f4re2qdkups7ijiw1.PNG)

I hope by just looking at the screenshots you can already see the pattern then you can easily grasp as to why the laravel service-repository pattern is clean and sustainable. You can clone this CRUD sample at <a>https://github.com/jsafe00/laravel-service-repository</a>

I have created a tutorial with interface implementation on this pattern at https://josafebalili.vercel.app/laravel-service-repository-interface

Or you might check my CRUD implementation of repository pattern with Laravel 8 and Php 8 at <a>https://github.com/jsafe00/her-running-medals-api</a>

I've created a simple package for this. You might want to check it. 

<a>https://github.com/jsafe00/laravel-s-r-c</a>


Everything has a pattern, you just need to notice it. This goes with the saying that everything has beauty, but not everyone sees it.

May we see beauty in everything.  Beauty is in the eye of the grateful.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7j2obig24iyc64thzac2.jpg)