PHPackages                             giordanolima/eloquent-repository - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. giordanolima/eloquent-repository

ActiveLibrary[Database &amp; ORM](/categories/database)

giordanolima/eloquent-repository
================================

Repository pattern for Eloquent ORM with a powerfull cache for database queries.

2.2.1(2y ago)301.5k4[3 issues](https://github.com/giordanolima/eloquent-repository/issues)MITPHPPHP &gt;=5.4.0

Since Feb 24Pushed 2y ago2 watchersCompare

[ Source](https://github.com/giordanolima/eloquent-repository)[ Packagist](https://packagist.org/packages/giordanolima/eloquent-repository)[ RSS](/packages/giordanolima-eloquent-repository/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (3)Versions (35)Used By (0)

Eloquent Repository
===================

[](#eloquent-repository)

[![Latest Stable Version](https://camo.githubusercontent.com/45d0e4356ae9939e7cac7fd96aa80ebf298456bb8f16c171e812d4f7b4bcba75/68747470733a2f2f706f7365722e707567782e6f72672f67696f7264616e6f6c696d612f656c6f7175656e742d7265706f7369746f72792f762f737461626c65)](https://packagist.org/packages/giordanolima/eloquent-repository)[![Total Downloads](https://camo.githubusercontent.com/8a1cc196c67302ec0dc3b48b2cac3aae37ab3f53dc72ce823ef30f5bf9360ce0/68747470733a2f2f706f7365722e707567782e6f72672f67696f7264616e6f6c696d612f656c6f7175656e742d7265706f7369746f72792f646f776e6c6f616473)](https://packagist.org/packages/giordanolima/eloquent-repository)[![License](https://camo.githubusercontent.com/5621c0d1d675a7ce25ad6c78e8f240b6182acbf3eafd378f22696f86908dd597/68747470733a2f2f706f7365722e707567782e6f72672f67696f7264616e6f6c696d612f656c6f7175656e742d7265706f7369746f72792f6c6963656e7365)](https://packagist.org/packages/giordanolima/eloquent-repository)[![StyleCI](https://camo.githubusercontent.com/66d0ff94a22d545d313c2d218c0e754c21040ff797d48d39425220428c00754e/68747470733a2f2f7374796c6563692e696f2f7265706f732f38323732393135362f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/82729156)

[Documentação em português.](README_PT.md)

Package to assist the implementation of the Repository Pattern using Eloquent ORM. The main feature is the flexibility and ease of use and a powerful driver for query caching.

Installation
------------

[](#installation)

Installing via Composer

```
composer require giordanolima/eloquent-repository
```

To configure the package options, declare the Service Provider in the `config / app.php` file.

```
'providers' => [
    ...
    GiordanoLima\EloquentRepository\RepositoryServiceProvider::class,
],
```

> If you are using version 5.5 or higher of Laravel, the Service Provider is automatically recognized by Package Discover.

To publish the configuration file:

```
php artisan vendor:publish
```

### Usage

[](#usage)

To get started you need to create your repository class and extend the `BaseRepository` available in the package. You also have to set the *Model* that will be used to perform the queries. Example:

```
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
}
```

Now it is possible to perform queries in the same way as it is used in Elquent.

```
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }

    public function getAllUser(){
        return $this->all();
    }

    public function getByName($name) {
        return $this->where("name", $name)->get();
    }

    // You can create methods with partial queries
    public function filterByProfile($profile) {
        return $this->where("profile", $profile);
    }

    // Them you can use the partial queries into your repositories
    public function getAdmins() {
        return $this->filterByProfile("admin")->get();
    }
    public function getEditors() {
        return $this->filterByProfile("editor")->get();
    }

    // You can also use Eager Loading in queries
    public function getWithPosts() {
        return $this->with("posts")->get();
    }
}
```

To use the class, just inject them into the controllers.

```
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    protected function index(UserRepository $repository) {
        return $repository->getAdmins();
    }
}
```

The injection can also be done in the constructor to use the repository in all methods.

```
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    private $repository;
	public function __construct()(UserRepository $repository) {
        $this->repository = $repository;
    }

    public function index() {
        return $this->repository->getAllUsers();
    }

}
```

> The Eloquent/QueryBuilder methods are encapsulated as protected and are available just into the repository class. Declare your own public data access methods within the repository to access them through the controller.

#### Paginate

[](#paginate)

As default value, the `paginate` method uses, *15* records per page. This default value can be set in the configuration file to be used in all repositories. If necessary, you can change the default value for a single repository overwriting the `perPage` property with the desired value.

```
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected $perPage = 10;
    protected function model() {
        return \App\User::class;
    }
}
```

#### OrderBy

[](#orderby)

You can declare a field and a default direction to be used for all queries in a particular repository. You can still choose other fields to sort or just skip the sorting methods.

```
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected $orderBy = "created_at";
    protected $orderByDirection = "DESC";
	protected function model() {
        return \App\User::class;
    }

    public function getAllUser(){
        // This query will use the default ordering of the repository.
        return $this->all();
    }

    public function getByName($name) {
        // In this query only the declared sort will be used.
        return $this->orderBy("name")->where("name", $name)->get();
    }

    // É possível criar métodos com consultas parciais
    public function getWithoutOrder() {
        // In this query, no sort will be used.
        return $this->skipOrderBy()->all();
    }

}
```

#### GlobalScope

[](#globalscope)

You can set a scope to use for all queries used in the repository. If necessary, you can also ignore this global scope.

```
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class AdminRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
    protected function globalScope() {
        return $this->where('is_admin', true);
    }

    public function getAdmins() {
        // In this query the declared global scope will be included.
        return $this->all();
    }

    public function getAll() {
        // In this query the declared global scope will not be included.
        return $this->skipGlobalScope()->all();
    }
}
```

Cache
-----

[](#cache)

The package comes with a powerful cache driver. The idea is that once the query is done, it will be cached. After the cache is done, it is possible to reduce the number of accesses to the database to zero. All caching is performed using the cache driver configured for the application. To use the driver, just use the trait that implements it.

```
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
    use CacheableRepository;
    protected function model() {
        return \App\User::class;
    }
}
```

Usage remains the same, with all cache management logic done automatically.

```
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    private $repository;
	public function __construct()(UserRepository $repository) {
        $this->repository = $repository;
    }

    public function index() {
        $users = $this->repository->getAllUsers();
        // if you call the same query later (even in other requests)
        // the query is already cached and you do not need to access the database again.
        $users = $this->repository->getAllUsers();
    }

}
```

Everytime the database data is changed, the cache is automatically cleaned to avoid outdated data. However, if it is necessary to clear the cache, it can be performed through the `clearCache()` method. You can also force access to the database and avoid cached data by using the `skipCache()` method.

```
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
    use CacheableRepository;
	protected function model() {
        return \App\User::class;
    }

    public function createUser($data) {
        // After inserting the data, the cache
        // is cleaned automatically.
        return $this->create($data);
    }

    public function addRules($user, $rules) {
        $user->rules()->attach($rules);
        $this->clearCache(); // Forcing cache cleaning.
    }

    public function getWithoutCache() {
        $this->skipCache()->all(); // Finding the data without using the cache.
    }
}
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 98.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~74 days

Recently: every ~333 days

Total

32

Last Release

1046d ago

Major Versions

1.0.x-dev → 2.0.02018-01-26

### Community

Maintainers

![](https://www.gravatar.com/avatar/961a7e8f71b1e25611c4156995cdf0c064024dd629479c31b92f938035ee797b?d=identicon)[giordanolima](/maintainers/giordanolima)

---

Top Contributors

[![giordanolima](https://avatars.githubusercontent.com/u/8314190?v=4)](https://github.com/giordanolima "giordanolima (69 commits)")[![Dylan-DPC](https://avatars.githubusercontent.com/u/99973273?v=4)](https://github.com/Dylan-DPC "Dylan-DPC (1 commits)")

---

Tags

eloquentlaravellaravel-packagerepositoryrepository-patternlaraveleloquentrepository

### Embed Badge

![Health badge](/badges/giordanolima-eloquent-repository/health.svg)

```
[![Health](https://phpackages.com/badges/giordanolima-eloquent-repository/health.svg)](https://phpackages.com/packages/giordanolima-eloquent-repository)
```

###  Alternatives

[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
