Abstracting common queries in your Laravel applications can be done in many ways. Let’s take a look at the simplest way using the “Repository pattern”.

The “Repository pattern” is essentially another level of abstraction on top of your database abstraction layer. In the context of Laravel, these are simple classes with methods that call more complex chains of methods on your model classes.

Generally, each repository is responsible for one entity within your application. For example, a UserRepository should only be responsible for retrieving User records.

Abstract implementation

I’ve seen various people use traits to implement repositories directly inside of their models. Personally I think this gives the model too much responsibility, especially since models in Laravel are essentially “God classes” already.

Instead, I’ll create an abstract class that all of the repository classes will extend:

<?php

namespace App\Repositories;

abstract class Repository
{
    //
}

#laravel

Simple Repositories in Laravel
2.45 GEEK