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

ActiveLibrary

bradleykingdev/laravel-repository
=================================

Repository for Laravel

015PHP

Since Mar 26Pushed 5y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#laravel-repository)

[![Latest Stable Version](https://camo.githubusercontent.com/6cca39cbadc74dd15fa2a4a1c9328bb6fc588c71f37a036dc17a994f200b31a6/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f427261646c65794b696e674465762f6c61726176656c2d7265706f7369746f72792e737667)](https://packagist.org/packages/BradleyKingDev/laravel-repository)

Install
-------

[](#install)

Via Composer

```
composer require BradleyKingDev/laravel-repository
```

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

```
BradleyKingDev\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 `BradleyKingDev\Repository\BaseRepository`, `BradleyKingDev\Repository\ExtendedRepository` or `BradleyKingDev\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 `BradleyKingDev\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.

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d332960cfe5bdd385c4efd59c61b0f4f73872e3e1004ea077a31b32c4bf2470?d=identicon)[bradleyk-dev](/maintainers/bradleyk-dev)

---

Top Contributors

[![bradleyking-dev](https://avatars.githubusercontent.com/u/57000861?v=4)](https://github.com/bradleyking-dev "bradleyking-dev (5 commits)")

### Embed Badge

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

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

PHPackages © 2026

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