PHPackages                             sindria/laravel-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. sindria/laravel-repository

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

sindria/laravel-repository
==========================

Repository for Laravel (fork of czim/laravel-repository)

2.0.3(6y ago)049MITPHPPHP &gt;=7.1.3

Since Aug 29Pushed 6y agoCompare

[ Source](https://github.com/SindriaInc/laravel-repository)[ Packagist](https://packagist.org/packages/sindria/laravel-repository)[ Docs](https://github.com/SindriaInc/laravel-repository)[ RSS](/packages/sindria-laravel-repository/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (8)Versions (32)Used By (0)

Laravel Repository
==================

[](#laravel-repository)

[![Latest Version on Packagist](https://camo.githubusercontent.com/efb43026175530adfdb248757ef1134cd9bc91cb48cb4876575d192efdf1297a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f637a696d2f6c61726176656c2d7265706f7369746f72792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/czim/laravel-repository)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/00d890d561343befffcca571332cafd30f093c2774b2aeb095b812d3f45d5bd5/68747470733a2f2f7472617669732d63692e6f72672f637a696d2f6c61726176656c2d7265706f7369746f72792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/czim/laravel-repository)[![Latest Stable Version](https://camo.githubusercontent.com/edcd5b5a9ff3e5acfce57d246a2b0bd1f2047545ef632dc43a2fbc20d57d3b85/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f637a696d2f6c61726176656c2d7265706f7369746f72792e737667)](https://packagist.org/packages/czim/laravel-repository)[![SensioLabsInsight](https://camo.githubusercontent.com/65eaee64ec95626230444dcada917b03ad49ae2ec62d3a94950e756b75fc779b/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f30323961623933302d393036342d346163662d383630322d3837663863303130663338372f6d696e692e706e67)](https://insight.sensiolabs.com/projects/029ab930-9064-4acf-8602-87f8c010f387)

Repository setup inspired by the Bosnadev/Repository package. This package is an extended, adjusted (but entirely independent) version of that, with its own interfaces.

One major difference to the Bosnadev repository is that this one is able to deal with repeated and varying calls to the same repository instance, without breaking down or undesirable repeated application of Criteria. You can instantiate a repository once and do anything with it in any order, and both queries and model manipulation methods will keep working.

Among the added functionality is the ability to override or 'temporarily' set and remove Criteria, post-processing models after retrieval.

I'm well aware that there is *much* to say against using Repositories like this (and the repository pattern in general), but I find they have their uses. I prefer using them to make for easier unit testing in large projects.

Version Compatibility
---------------------

[](#version-compatibility)

LaravelPackage5.11.0.x5.21.2.x5.31.2.x5.4 to 5.81.4.x6.0 and up2.xInstall
-------

[](#install)

Via Composer

```
$ composer require czim/laravel-repository
```

If you want to use the repository generator through the `make:repository` Artisan command, add the `RepositoryServiceProvider` to your `config/app.php`:

```
Czim\Repository\RepositoryServiceProvider::class,
```

Publish the repostory configuration file.

```
php artisan vendor:publish --tag="repository"
```

Basic Usage
-----------

[](#basic-usage)

Simply extend the (abstract) repository class of your choice, either `Czim\Repository\BaseRepository`, `Czim\Repository\ExtendedRepository` or `Czim\Repository\ExtendedPostProcessingRepository`.

The only abstract method that must be provided is the `model` method (this is just like the way Bosnadev's repositories are used).

### Make Repository

[](#make-repository)

The `make:repository` command automatically creates a new Eloquent model repository class. It will also attempt to link the correct Eloquent model, but make sure to confirm that it is properly set up.

```
php artisan make:repository PostsRepository
```

The above command will create a repository class named PostsRepository and link the Post model to it.

If you want to set the related model explicitly, you can add the model class name:

```
php artisan make:repository PostsRepository "App\Models\AlternativePost"
```

### Base-, Extended- and PostProcessing

[](#base--extended--and-postprocessing)

Depending on what you require, three different abstract repository classes may be extended:

- `BaseRepository`

    Only has the retrieval and simple manipulation methods (`create()`, `update()` and `delete()`), and Criteria handling.
- `ExtendedRepository`

    Handles an **active** check for Models, which will by default exclude any model which will not have its `active` attribute set to true (configurable by setting `hasActive` and/or `activeColumn`). Handles caching, using [dwightwatson/rememberable](https://github.com/dwightwatson/rememberable) by default (but you can use your own Caching Criteria if desired). Allows you to set Model scopes, for when you want to use an Eloquent model scope to build your query.
- `ExtendedPostProcessingRepository`

    Just like Extended, but also allows for altering/decorating models after they are retrieved. By default, the only PostProcessor active is one that allows you to hide/unhide attributes on Models.

### Using the repository to retrieve models

[](#using-the-repository-to-retrieve-models)

Apart from the basic stuff (inspired by Bosnadev), there are some added methods for retrieval:

- `query()`: returns an Eloquent\\Builder object reflecting the active criteria, for added flexibility
- `count()`
- `first()`
- `findOrFail()`: just like `find()`, but throws an exception if nothing found
- `firstOrFail()`: just like `first()`, but throws an exception if nothing found

Every retrieval method takes into account the currently active Criteria (including one-time overrides), see below.

For the `ExtendedPostProcessingRepository` goes that postprocessors affect all models returned, and so are applied in all the retrieval methods (`find()`, `firstOrFail()`, `all()`, `allCallback`, etc). The `query()` method returns a Builder object and therefore circumvents postprocessing. If you want to manually use the postprocessors, simply call `postProcess()` on any Model or Collection of models.

#### Handling Criteria

[](#handling-criteria)

Just like Bosnadev's repository, Criteria may be pushed onto the repository to build queries. It is also possible to set default Criteria for the repository by overriding the `defaultCriteria()` method and returning a Collection of Criteria instances.

Criteria may be defined or pushed onto the repository by **key**, like so:

```
    $repository->pushCriteria(new SomeCriteria(), 'KeyForCriteria');
```

This allows you to later remove the Criteria by referring to its key:

```
    // you can remove Criteria by key
    $repository->removeCriteria('KeyForCriteria');
```

To change the Criteria that are to be used only for one call, there are helper methods that will preserve your currently active Criteria. If you use any of the following, the active Criteria are applied (insofar they are not removed or overridden), and additional Criteria are applied only for the next retrieval method.

```
    // you can push one-time Criteria
    $repository->pushCriteriaOnce(new SomeOtherCriteria());

    // you can override active criteria once by using its key
    $repository->pushCriteriaOnce(new SomeOtherCriteria(), 'KeyForCriteria');

    // you can remove Criteria *only* for the next retrieval, by key
    $repository->removeCriteriaOnce('KeyForCriteria');
```

Note that this means that *only* Criteria that have keys can be removed or overridden this way. A `CriteriaKey` Enum is provided to more easily refer to the standard keys used in the `ExtendedRepository`, such as 'active', 'cache' and 'scope'.

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

[](#configuration)

No configuration is required to start using the repository. You use it by extending an abstract repository class of your choice.

### Extending the classes

[](#extending-the-classes)

Some properties and methods may be extended for tweaking the way things work. For now there is no documentation about this (I will add some later), but the repository classes contain many comments to help you find your way (mainly check the `ExtendedRepository` class).

### Traits

[](#traits)

Additionally, there are some traits that may be used to extend the functionality of the repositories, see `Czim\Repository\Traits`:

- `FindsModelsByTranslationTrait` (only useful in combination with the [dimsav/laravel-translatable](https://github.com/dimsav/laravel-translatable) package)
- `HandlesEloquentRelationManipulationTrait`
- `HandlesEloquentSavingTrait`
- `HandlesListifyModelsTrait` (only useful in combination with the [lookitsatravis/listify](https://github.com/lookitsatravis/listify) package)

I've added these mainly because they may help in using the repository pattern as a means to make unit testing possible without having to mock Eloquent models.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Coen Zimmerman](https://github.com/czim)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor3

3 contributors hold 50%+ of commits

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 ~62 days

Recently: every ~193 days

Total

28

Last Release

2235d ago

Major Versions

1.4.4 → 2.0.02019-09-18

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

1.2.0PHP &gt;=5.6.0

2.0.0PHP &gt;=7.1.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/88d66fa70f20a8eb03723d4fa614f8f2d70e4dd2d6d97f6cf22fdb3f542d19c7?d=identicon)[lucapitzoi](/maintainers/lucapitzoi)

---

Top Contributors

[![czim](https://avatars.githubusercontent.com/u/11831617?v=4)](https://github.com/czim "czim (9 commits)")[![mvdstam](https://avatars.githubusercontent.com/u/7629384?v=4)](https://github.com/mvdstam "mvdstam (5 commits)")[![ionut-tanasa](https://avatars.githubusercontent.com/u/44393839?v=4)](https://github.com/ionut-tanasa "ionut-tanasa (4 commits)")[![lucapitzoi](https://avatars.githubusercontent.com/u/35993148?v=4)](https://github.com/lucapitzoi "lucapitzoi (2 commits)")[![freefcw](https://avatars.githubusercontent.com/u/417011?v=4)](https://github.com/freefcw "freefcw (2 commits)")[![douglasresendemaciel](https://avatars.githubusercontent.com/u/11336192?v=4)](https://github.com/douglasresendemaciel "douglasresendemaciel (2 commits)")[![mattiabasone](https://avatars.githubusercontent.com/u/3951536?v=4)](https://github.com/mattiabasone "mattiabasone (2 commits)")[![spelcaster](https://avatars.githubusercontent.com/u/3278419?v=4)](https://github.com/spelcaster "spelcaster (1 commits)")[![daniel-de-wit](https://avatars.githubusercontent.com/u/3015394?v=4)](https://github.com/daniel-de-wit "daniel-de-wit (1 commits)")[![dr-de-wit](https://avatars.githubusercontent.com/u/115578142?v=4)](https://github.com/dr-de-wit "dr-de-wit (1 commits)")

---

Tags

laraveldatabaseeloquentrepositoryrepositoriescriteria

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sindria-laravel-repository/health.svg)

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

###  Alternatives

[czim/laravel-repository

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

54110.0k4](/packages/czim-laravel-repository)

PHPackages © 2026

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