Traits

Are an alternative approach to inheritance that solves some limitations of single class inheritance, which PHP uses. Since all Eloquent models extends Model it can not extend another class. This is commonly used to share similar logic across objects. Let's imagine a couple of models have a Company relationship.

trait HasCompany {
   public function company() {
       return $this->belongsTo(Company::class);
   }
}

Now the user can easily share code from the trait, by the keyword using. This is an example and most often a more complex use case would be needed for it to make sense.

class User extends Model {
   use HasCompany;
}

Repositories

Repositories is a design pattern to abstract data layers from the application. Your logic should not care about how you store data, so if you wanted to change from Mysql to Mongodb, you would only swap out the repository and not have to change business logic.

Very opinionated here, but this is not a fitting design pattern for Laravel. Laravel has Eloquent and the database layer is therefor already abstracted. Repositories is sometimes used for Laravel applications, but rather an outlier, than a common sight. One of the main reasons for repositories, is to be data implementation agnostic, which already exists and you can swap between SQL servers flawlessly. Also Eloquents features like ::find(), scopes etc. feels like a replacement for Repositories and is quirky to use at the same time.

If you use Doctrine as the ORM, which you can in Laravel, it is the core of their architecture and repositories should be used.

It sometimes occurs that the repository pattern is used to encapsulate business logic, with a similar approach as used with services or action pattern.

Services

Is commonly used for a place to store business logic or the building blocks of your actions in your application. In traditional MVC design, Controllers should only handle input. Normally you would put your logic in Models, but they get "fat" very quickly, when this happens services is a common place to put business logic. Sometimes action or command design pattern are used, which is similar but still different.

One of the core things it solves, is to make your business logic reusable. Imaging filtering all users by an active flag, when you retrieve it in its controller.

public function all() {
    return User::where('active', true)->get();
}

Now you have your business logic, that enforces that you only work on active users, later you want to notify all active users with an email, by notifications using a command.

class NotifyUsers extends Command {
    public function handle() {
        foreach (User::where('active', true)->get() as $user) {
            $user->notify();
        }
    }
}

Now you manually have to keep business logic up to date. Next time you add a second condition or change the logic, you have to change the code in two places. In a big application where this code block is used often, it can make it quite hard to maintain the conditions without forgetting one of the places. If you make a service with this logic, you can easily utilize the same business logic across the application. While one have one place to change the code, if this logic had to change.

class UserService {
    public function all() {
        return User::where('active', true)->get();
    }
}

Everywhere you want to use this business logic getting active users, you can use the service. Therefor only having one place to maintain the logic. A call can be as simply as resolve(UserService::class)->all(). Example of the updated logic with services would be.

// controller
public function all(UserService $userService) {
    return $userService->all();
}

// command
class NotifyUsers extends Command {
    public function handle(UserService $userService) {
        $userService->all()->each->notify();
    }
}

Conclusion

The world is not black and white, you have to figure out your own approach. My advice is, do not spend time on Repositories, Laravel has a lot of features to handle data related operations scopes, getters setters etc. that conflicts with the Repository design pattern. See if a service like design approach suits you and you can utilize em. Traits is not as much an architectural design pattern, as it is a Class inheritance alternative, simply to share logic between classes.

Answer from mrhn on Stack Overflow
🌐
Reddit
reddit.com › r/laravel › mastering the service-repository pattern in laravel
r/laravel on Reddit: Mastering the Service-Repository Pattern in Laravel
June 29, 2024 - The model already serves as a repository, it has all of the usual repository stuff built in and provides a place to add what you need. Additional repostiories make sense only in some edge cases where your business "model" actually needs to manipulate multiple database/eloquent models. ... Garbage design pattern in Laravel. Burn it to the ground, never look back. ... To share another POV for the use of Services and Repositories...
🌐
GitHub
github.com › yaza-putu › laravel-repository-with-service
GitHub - yaza-putu/laravel-repository-with-service: With repository and service you can separate business logic and query logic, slim controller and DRY. Simple generate repository and service with artisan command, automatically bind interface to class implement with IOC container and dependecy injection (SOLID) · GitHub
With repository and service you can separate business logic and query logic, slim controller and DRY. Simple generate repository and service with artisan command, automatically bind interface to class implement with IOC container and dependecy injection (SOLID) - yaza-putu/laravel-repository-with-service
Starred by 143 users
Forked by 34 users
Languages   PHP 92.2% | Shell 7.8%
Discussions

php - When to use Repository vs Service vs Trait in Laravel? - Stack Overflow
To avoid code duplication in Laravel, I want to have a method that is used by multiple Controllers, it inserts some rows in database and also updates some data in another table. I thought of using More on stackoverflow.com
🌐 stackoverflow.com
php - What is the purpose of repository when service classes can do the same? - Stack Overflow
Repository pattern is for speaking with database so we need repository pattern for big scale Laravel projects. More on stackoverflow.com
🌐 stackoverflow.com
Service vs Repository | Laravel.io
The Laravel portal for problem solving, knowledge sharing and community building. More on laravel.io
🌐 laravel.io
php - Creating Laravel repositories and binding as service providers - Stack Overflow
Depends on how you design your repositories, you can instantiate them manually, inject them into controller's instantiation (in the __contruct(UserRepository $userRepository)) as you can see in laravel from scratch tutorial from laracast or use them as service providers. More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › adobrovolsky97 › laravel-repository-service-pattern
GitHub - adobrovolsky97/laravel-repository-service-pattern: Laravel Repository-Service Pattern
The purpose of using the repository service pattern in Laravel is to decouple the application's business logic from the specific implementation details of data storage. By doing so, it promotes code reusability, maintainability, and testability.
Starred by 28 users
Forked by 6 users
Languages   PHP 100.0% | PHP 100.0%
Top answer
1 of 1
102

Traits

Are an alternative approach to inheritance that solves some limitations of single class inheritance, which PHP uses. Since all Eloquent models extends Model it can not extend another class. This is commonly used to share similar logic across objects. Let's imagine a couple of models have a Company relationship.

trait HasCompany {
   public function company() {
       return $this->belongsTo(Company::class);
   }
}

Now the user can easily share code from the trait, by the keyword using. This is an example and most often a more complex use case would be needed for it to make sense.

class User extends Model {
   use HasCompany;
}

Repositories

Repositories is a design pattern to abstract data layers from the application. Your logic should not care about how you store data, so if you wanted to change from Mysql to Mongodb, you would only swap out the repository and not have to change business logic.

Very opinionated here, but this is not a fitting design pattern for Laravel. Laravel has Eloquent and the database layer is therefor already abstracted. Repositories is sometimes used for Laravel applications, but rather an outlier, than a common sight. One of the main reasons for repositories, is to be data implementation agnostic, which already exists and you can swap between SQL servers flawlessly. Also Eloquents features like ::find(), scopes etc. feels like a replacement for Repositories and is quirky to use at the same time.

If you use Doctrine as the ORM, which you can in Laravel, it is the core of their architecture and repositories should be used.

It sometimes occurs that the repository pattern is used to encapsulate business logic, with a similar approach as used with services or action pattern.

Services

Is commonly used for a place to store business logic or the building blocks of your actions in your application. In traditional MVC design, Controllers should only handle input. Normally you would put your logic in Models, but they get "fat" very quickly, when this happens services is a common place to put business logic. Sometimes action or command design pattern are used, which is similar but still different.

One of the core things it solves, is to make your business logic reusable. Imaging filtering all users by an active flag, when you retrieve it in its controller.

public function all() {
    return User::where('active', true)->get();
}

Now you have your business logic, that enforces that you only work on active users, later you want to notify all active users with an email, by notifications using a command.

class NotifyUsers extends Command {
    public function handle() {
        foreach (User::where('active', true)->get() as $user) {
            $user->notify();
        }
    }
}

Now you manually have to keep business logic up to date. Next time you add a second condition or change the logic, you have to change the code in two places. In a big application where this code block is used often, it can make it quite hard to maintain the conditions without forgetting one of the places. If you make a service with this logic, you can easily utilize the same business logic across the application. While one have one place to change the code, if this logic had to change.

class UserService {
    public function all() {
        return User::where('active', true)->get();
    }
}

Everywhere you want to use this business logic getting active users, you can use the service. Therefor only having one place to maintain the logic. A call can be as simply as resolve(UserService::class)->all(). Example of the updated logic with services would be.

// controller
public function all(UserService $userService) {
    return $userService->all();
}

// command
class NotifyUsers extends Command {
    public function handle(UserService $userService) {
        $userService->all()->each->notify();
    }
}

Conclusion

The world is not black and white, you have to figure out your own approach. My advice is, do not spend time on Repositories, Laravel has a lot of features to handle data related operations scopes, getters setters etc. that conflicts with the Repository design pattern. See if a service like design approach suits you and you can utilize em. Traits is not as much an architectural design pattern, as it is a Class inheritance alternative, simply to share logic between classes.

🌐
DEV Community
dev.to › safventure11000 › implement-crud-with-laravel-service-repository-pattern-1dkl
Implement CRUD with Laravel Service-Repository Pattern - DEV Community
June 27, 2025 - Repository 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. We call the Post model in the constructor of our class.
Find elsewhere
🌐
Medium
joe-wadsworth.medium.com › laravel-repository-service-pattern-acf50f95726
Repository & Service Pattern in Laravel | by Joe Wadsworth | Medium
January 26, 2021 - The idea of the repository and service pattern are to keep the business logic (services) and the data access logic (repositories) of your app contained in their own sections.
🌐
Krodox
krodox.com › blog › services-vs-repositories
Understanding the Difference Between Repository and Service in Laravel | Krodox Official Web site
November 6, 2024 - Repositories are typically interfaces and classes that provide an abstraction layer for querying and persisting data. They focus on CRUD operations (Create, Read, Update, Delete) and handle interactions with the model layer. Decouples database logic from the application logic.
🌐
Medium
medium.com › @devajayantha › laravel-service-repository-pattern-my-experience-and-key-benefits-afa985cd8b18
Laravel Service Repository Pattern: My Experience and Key Benefits | by Deva Jayantha | Medium
February 21, 2025 - This article explain a simple implementation of the Repository-Service Pattern in Laravel to keep the code organized and scalable. By separating logic into services and handling the database through repositories, this pattern enhances cleanliness, flexibility, and readability of the code.
🌐
GitHub
github.com › jsafe00 › laravel-service-repository
GitHub - jsafe00/laravel-service-repository: Laravel-Service-Repository pattern · GitHub
Laravel app using service-repository pattern. You may use Postman to try the CRUD functionality.
Starred by 120 users
Forked by 50 users
Languages   PHP 96.3% | Blade 3.0% | Shell 0.7%
Top answer
1 of 3
15

There is no definitive answer for your question since the patterns you use highly depend on the project's complexity and needs.

However, a Service and a Repository are two different things. The Repository is a common wrapper for the model and is where you write the queries to the database. IMO you shouldn't add logic here and the sole purpose of a repository is to grab os store data into the database. The advantage of Repositories is the "easiness" to switch to other database systems.

A Service, IMO, is where you add all the application's logic.

For additional information refer to this answer.

2 of 3
15

From DDD (Domain Driven Design) the responsibility of a repository is to take care of loading, storing, modifying and deleting an entity on the designated data storage (which may or may not be a database -- it may even be a remote server or just a file).

A service, on the other hand, has (or should have) a very narrow responsibility of performing some useful activity. Each service is instantiated separately and then injected into code in the application layer or above, which acts as a bridge (Bridge pattern). This approach has proven to be very advantageous because it allows to manage the dependencies between otherwise unrelated (uncoupled) code.

Those two definitions and the origin of the concepts shows that they actually are very different things. By pure chance you noticed that a repository and a service have an apparent overlap, but that's due to implementation details or plain misuse. Their responsibilities may under circumstances go hand in hand (giving rise to a collaboration) but they really are orthogonal concepts.

Furthermore, Repositories should arise from a deep layer (Persistance or DAL, Data Access Layer). Services, on the other hand, often are vertical cross-cutting or arise on the application layer.

Through proper layering the differences between repositories and services become even more apparent.

Do not think about them as pure code artifacts you can move around. They are well-defined concepts useful to understand about and design the structure of a system. They decline into actual code only as a consequence of that design.

I hope I succeeded in writing something that clears up some ideas and is not confusing.

🌐
Laravel.io
laravel.io › forum › 05-11-2014-service-vs-repository
Service vs Repository | Laravel.io
Repository methods could also contain logic on how to get or set data. Last updated 3 years ago. ... You should read Taylor's book, all your questions will be answered. Last updated 3 years ago. ... Service: A class that operates a business logic that doesn't belong to a specific business object.
🌐
Medium
medium.com › @binumathew1988 › mastering-the-service-repository-pattern-in-laravel-751da2bd3c86
Mastering the Service-Repository Pattern in Laravel | by Binu Mathew | Medium
September 2, 2024 - The Service-Repository pattern isn’t just about following best practices — it’s about setting your Laravel application up for long-term success.
Top answer
1 of 2
3

why doesn't Laravel have a built-in repository pattern

Because there is no consensus on how they should be used if used at all.

For example, I use Repositories as an intermediary between laravel models and laravel controllers that needs the model instance to be instantiated and I never inject them into controllers but instantiate them manually when needed.

do we need to bind repositories as providers ?

As said above, there is no consensus so NO.

Depends on how you design your repositories, you can instantiate them manually, inject them into controller's instantiation (in the __contruct(UserRepository $userRepository)) as you can see in laravel from scratch tutorial from laracast or use them as service providers.

2 of 2
1

From Laravel Repository Pattern – How to use & why it matters

The last step is to register this service provider in our config/app.php. Open this file and add to providers our provider App\Providers\RepositoryServiceProvider::class

Now our application knows what class it should use when we type an objects by its interfaces.

This is why you need to bind your interfaces like that:

/** 
* Register services. 
* 
* @return void  
*/ 
public function register() 
{ 
   $this->app->bind(EloquentRepositoryInterface::class, BaseRepository::class);
   $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}

In order to let Laravel knows about what to instantiate when you do in your controller:

private $userRepository;

public function __construct(UserRepositoryInterface $userRepository)
{
   $this->userRepository = $userRepository;
}

[EDIT 1]

Also there is a great article from the Laravel documentation for Dependency Injection: laravel.com/docs/master/controllers#dependency-injection-and-controllers

🌐
ASPER BROTHERS
asperbrothers.com › home › product development › laravel repository pattern – php design pattern
Laravel Repository Pattern – PHP Design Pattern | ASPER BROTHERS
September 10, 2022 - A repository is a separation between a domain and a persistent layer. The repository provides a collection interface to access data stored in a database, file system, or external service.