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

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:

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

php artisan make:model Post -mcr

-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


Alt Text

Model Please ensure that our attributes are fillable. Update Post model like below.

Alt Text


Then, update the post migration like below then execute

php artisan migrate

Alt Text


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

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


Like repository, laravel doesn’t have a service command. Create a Services folder, add PostService file and add the code below.

Alt Text

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.


PostController -> PostService -> PostRepository

Alt Text

$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

Alt Text

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



Alt TextAlt TextAlt Text


Alt TextAlt Text


Alt TextAlt TextAlt Text


Alt TextAlt TextAlt Text

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

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