PHPackages                             saritasa/laravel-repositories - 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. saritasa/laravel-repositories

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

saritasa/laravel-repositories
=============================

Saritasa repository pattern implementation for Laravel

4.0.0(1y ago)317.3k↓33.3%2[1 PRs](https://github.com/Saritasa/php-laravel-repositories/pulls)1MITPHPPHP &gt;=8.0

Since Apr 8Pushed 1y ago5 watchersCompare

[ Source](https://github.com/Saritasa/php-laravel-repositories)[ Packagist](https://packagist.org/packages/saritasa/laravel-repositories)[ RSS](/packages/saritasa-laravel-repositories/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (37)Used By (1)

Laravel Repositories
====================

[](#laravel-repositories)

[![PHP Unit](https://github.com/Saritasa/php-laravel-repositories/workflows/PHP%20Unit/badge.svg)](https://github.com/Saritasa/php-laravel-repositories/actions)[![PHP CodeSniffer](https://github.com/Saritasa/php-laravel-repositories/workflows/PHP%20Codesniffer/badge.svg)](https://github.com/Saritasa/php-laravel-repositories/actions)[![CodeCov](https://camo.githubusercontent.com/66c23d877248da0db2af496c0249e06d184f2e285392c59fc7d80b51c34630b4/68747470733a2f2f636f6465636f762e696f2f67682f53617269746173612f7068702d6c61726176656c2d7265706f7369746f726965732f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/Saritasa/php-laravel-repositories)[![Release](https://camo.githubusercontent.com/9929ae5fa982124ed2eae1b97e76e934552862c32ac2879b264cef27e9c38793/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73617269746173612f7068702d6c61726176656c2d7265706f7369746f726965732e737667)](https://github.com/Saritasa/php-laravel-repositories/releases)[![PHPv](https://camo.githubusercontent.com/874a3a129e210bf7c252b073343a6ac4728a2ed872bb6e79c7037988270ec4cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f73617269746173612f6c61726176656c2d7265706f7369746f726965732e737667)](http://www.php.net)[![Downloads](https://camo.githubusercontent.com/79bb974268b60907fc5412b745ba117e5b60ef8cdf721a4176cfd648844c01ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73617269746173612f6c61726176656c2d7265706f7369746f726965732e737667)](https://packagist.org/packages/saritasa/laravel-repositories)

Implementation of Repository pattern for Laravel (on top of Eloquent)

Laravel 5.5+
------------

[](#laravel-55)

Install the `saritasa/laravel-repositories` package:

```
$ composer require saritasa/laravel-repositories
```

Configuration
-------------

[](#configuration)

- Publish configuration file:

```
php artisan vendor:publish --tag=laravel_repositories
```

- Register custom repositories implementation:

```
return [
	'bindings' => [
	    \App\Models\User::class => \App\Repositories\UserRepository::class,
	],
];
```

*Note: Your custom repository must implement `IRepository` contract.*

Getting repository inside your code
-----------------------------------

[](#getting-repository-inside-your-code)

To get specific repository in your code you can just build with DI container repositories factory and then build needed for your repository in this factory.
**Example:**

```
    $repositoryFactory = app(\Saritasa\LaravelRepositories\Repositories\IRepositoryFactory::class);
    $userRepository = $repositoryFactory->getRepository(\App\Models\User::class);
```

Filtering results with repository
---------------------------------

[](#filtering-results-with-repository)

Methods findWhere/getWhere/getWith/getPage/getCursorPage/count/ can receive criteria as params Here the examples of available syntax:

- Criterion without operator:

```
    $criteria = [
        'field1' => 'value1',
        'field2' => 1,
    ];
```

*In this case `=` operator and `and` boolean between them will be used.*
**Example:** `... 'field1 = 'value1' and 'field2' = 1 ...`

- Criterion with operator:

```
    $criteria = [
        ['field1', '', 'value1'],
        ['field2', '>', 1, 'or'],
        ['field3', 'in', [1, 2]],
        ['field4', 'not in', new \Illuminate\Support\Collection([1, 2])],
    ];
```

**`Important:` arrays and collection can be used only with `in` and `not in` operators.**
*Note: As 4th parameter you can pass boolean `or`/`and` (`and` uses by default). But you should remember that boolean used between current and previous criterion*
**Example:** `... 'field1  'value1' or 'field2' > 1 and 'field3' in (1, 2) and 'field4' not in (1, 2) ...`

- Criterion as DTO:

```
    $criteria = [
            new Criterion([
                Criterion::OPERATOR => '',
                Criterion::VALUE => 'value1',
                Criterion::ATTRIBUTE => 'field1',
            ]),
            new Criterion([
                Criterion::OPERATOR => '>',
                Criterion::VALUE => 1,
                Criterion::ATTRIBUTE => 'field2',
                Criterion::BOOLEAN => 'or',
            ]),
            new Criterion([
                Criterion::OPERATOR => 'in',
                Criterion::VALUE => [1, 2],
                Criterion::ATTRIBUTE => 'field3',
            ]),
            new Criterion([
                Criterion::OPERATOR => 'not in',
                Criterion::VALUE => [1, 2],
                Criterion::ATTRIBUTE => 'field4',
            ]),
    ];
```

Result will be the same as in previous example.

- Nested criteria:
    You can group different conditions that gives flexibility in getting data from repository

```
    $criteria = [
        [
            ['field1', '', 'value1'],
            ['field2', '>', 1, 'or'],
        ],
        [
            ['field3', 'in', [1, 2]],
            ['field4', 'not in', [1, 2],
            'boolean' => 'or',
        ],
    ];
```

*Note: you can add nesting level in any depth what you want. To use `or` condition between one group and other(group and non-group condition) you can pass 'boolean' parameter in the same level as other conditions.*

**Example:**`... ('field1  'value1' or 'field2' > 1) or ('field3' in (1, 2) and 'field4' not in (1, 2)) ...`

- Relation existence criterion:
    You can build queries to check on existence any model relations

```
    $criteria = [
        new RelationCriterion('roles', [['slug', 'in', [1, 2]]], 'or'),
    ];
```

Preload model relations
-----------------------

[](#preload-model-relations)

Method **getWith()** method allows to retrieve list of entities with
eager loaded related models and related models counts. Also allows to filter this list by given criteria
and sort in requested order.

**Example**:

```
$usersRepository->getWith(
    ['role', 'supervisors'],
    ['phones'],
    [],
    new Saritasa\LaravelRepositories\DTO\SortOptions('name', 'DESC')
);
```

- Each user will be retrieved with pre-loaded role and supervisors models.
- Each user will be retrieved with pre-loaded phones relation count.
- List of users will be ordered by requested sort options.

Exceptions
----------

[](#exceptions)

### Repository Exception

[](#repository-exception)

Base exception for repository layer.

### Repository register exception

[](#repository-register-exception)

Throws when can not register custom repository.

### Model not found Exception

[](#model-not-found-exception)

Throws in case when some model not exists in storage.

### Bad Criteria Exception

[](#bad-criteria-exception)

Throws when provided criteria has incorrect format at least in one criterion inside.

Contributing
------------

[](#contributing)

1. Create fork, checkout it
2. Develop locally as usual. **Code must follow [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/)** -
    run [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) to ensure, that code follows style guides
3. **Cover added functionality with unit tests** and run [PHPUnit](https://phpunit.de/) to make sure, that all tests pass
4. Update [README.md](README.md) to describe new or changed functionality
5. Add changes description to [CHANGES.md](CHANGES.md) file. Use [Semantic Versioning](https://semver.org/) convention to determine next version number.
6. When ready, create pull request

### Make shortcuts

[](#make-shortcuts)

If you have [GNU Make](https://www.gnu.org/software/make/) installed, you can use following shortcuts:

- `make cs` (instead of `php vendor/bin/phpcs`) -
    run static code analysis with [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)
    to check code style
- `make csfix` (instead of `php vendor/bin/phpcbf`) -
    fix code style violations with [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)
    automatically, where possible (ex. PSR-2 code formatting violations)
- `make test` (instead of `php vendor/bin/phpunit`) -
    run tests with [PHPUnit](https://phpunit.de/)
- `make install` - instead of `composer install`
- `make all` or just `make` without parameters -
    invokes described above **install**, **cs**, **test** tasks sequentially -
    project will be assembled, checked with linter and tested with one single command

Resources
---------

[](#resources)

- [Bug Tracker](http://github.com/saritasa/php-laravel-repositories/issues)
- [Code](http://github.com/saritasa/php-laravel-repositories)
- [Changes History](CHANGES.md)
- [Authors](http://github.com/saritasa/php-laravel-repositories/contributors)

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 79.2% 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 ~78 days

Recently: every ~469 days

Total

35

Last Release

644d ago

Major Versions

1.0.16 → 2.0.02018-02-13

2.x-dev → 3.0.02018-05-25

3.8.0 → 4.0.02024-08-07

PHP version history (3 changes)1.0.0PHP &gt;=7.0

3.0.0PHP &gt;=7.1

4.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7632cf2eb5dd552ace9d66947ed6919e19c6a2fdb5dc73b8c71e0f9886ccc83?d=identicon)[saritasa](/maintainers/saritasa)

---

Top Contributors

[![populov](https://avatars.githubusercontent.com/u/3766033?v=4)](https://github.com/populov "populov (57 commits)")[![veeeeeRySeXy](https://avatars.githubusercontent.com/u/10694924?v=4)](https://github.com/veeeeeRySeXy "veeeeeRySeXy (9 commits)")[![hollow-en](https://avatars.githubusercontent.com/u/87475798?v=4)](https://github.com/hollow-en "hollow-en (3 commits)")[![maxermolenko](https://avatars.githubusercontent.com/u/11438109?v=4)](https://github.com/maxermolenko "maxermolenko (3 commits)")

---

Tags

laravelphplaraveldatabaserepository

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/saritasa-laravel-repositories/health.svg)

```
[![Health](https://phpackages.com/badges/saritasa-laravel-repositories/health.svg)](https://phpackages.com/packages/saritasa-laravel-repositories)
```

###  Alternatives

[illuminatech/config

Provides support for Laravel application runtime configuration managed in persistent storage

14921.0k1](/packages/illuminatech-config)[czim/laravel-repository

Repository for Laravel (inspired by and indebted to Bosnadev/Repositories)

54110.0k4](/packages/czim-laravel-repository)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)[dragon-code/laravel-data-dumper

Adding data from certain tables when executing the `php artisan schema:dump` console command

3418.6k](/packages/dragon-code-laravel-data-dumper)

PHPackages © 2026

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