PHPackages                             ollieread/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ollieread/laravel-repositories

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

ollieread/laravel-repositories
==============================

A package that exists to show you that repositories don't have to be over complicated

4492[1 issues](https://github.com/ollieread/laravel-repositories/issues)[1 PRs](https://github.com/ollieread/laravel-repositories/pulls)PHP

Since Aug 28Pushed 7y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#laravel-repositories)

[![Latest Stable Version](https://camo.githubusercontent.com/8af8248ca13a1f238587f321ac84b4adf647a8ee98fe7c2eb940e5bf44f1aea9/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d7265706f7369746f726965732f762f737461626c652e706e67)](https://packagist.org/packages/ollieread/laravel-repositories) [![Total Downloads](https://camo.githubusercontent.com/4e9519cb8c108725e0a32fdcb6c42e7ed9917ce4b14fc4ae0002cb27b0d8ffec/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d7265706f7369746f726965732f646f776e6c6f6164732e706e67)](https://packagist.org/packages/ollieread/laravel-repositories) [![Latest Unstable Version](https://camo.githubusercontent.com/f18dc7dc3280129d39a105ea3c2b650e71ac70f39f91e5bb5dbfffe7c3ef9a3d/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d7265706f7369746f726965732f762f756e737461626c652e706e67)](https://packagist.org/packages/ollieread/laravel-repositories) [![License](https://camo.githubusercontent.com/4e932ca81bfe232c22feb1ce587e0a28333537713c71b5b194cadfe6e61b8ace/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d7265706f7369746f726965732f6c6963656e73652e706e67)](https://packagist.org/packages/ollieread/laravel-repositories)

- **Laravel**: 5.6
- **PHP**: 7.1+
- **Author**: Ollie Read
- **Author Homepage**:

Repositories don't need to be complicated, and this package is here to prove that.

FAQ
---

[](#faq)

Before we get into the specifics, here's a quick FAQ.

### What is the point of having a repository?

[](#what-is-the-point-of-having-a-repository)

The repository pattern exists to provide a level of abstraction between database/datasource interactions and the logic of the codebase.

### Doesn't Eloquent already cover this?

[](#doesnt-eloquent-already-cover-this)

Yes and No. Eloquent uses the Active Record pattern and as such, suffers a bit of a single concern crisis. Eloquent can do everything that you'd typically do in a repository, but that also means that there's no one place storing your queries. They're everywhere.

### What's wrong with queries everywhere?

[](#whats-wrong-with-queries-everywhere)

You've got a model, you're querying this model in 15 places. 13 of those places need an extra condition. You now need to dig out every query and update them. You can't add a global scope because not everything needs this. I promise you, more often than not you'll miss one.

### But why would I use repositories?

[](#but-why-would-i-use-repositories)

Repositories will group all of your database/datasource interactions into one central location. This means that as your codebase grows you know exactly where to go, and can easily get vision on a models entire interaction with the database/datasource.

### I don't want to define loads of interfaces

[](#i-dont-want-to-define-loads-of-interfaces)

Good, you shouldn't have to. Follow YAGNI. If you truly do end up switching out your database/datasource, having a bunch of interfaces isn't going to help at all. Sure you may think it will, but trust me. It doesn't. On top of that, it's very unlikely you'd ever even do this.

### But my repository will be massive

[](#but-my-repository-will-be-massive)

It shouldn't be. One of the great purposes of repositories is that it can help follow DRY. If you've got 15 almost identical queries, you probably only need one method with one or more arguments, sorted.

### If I expose all of the Eloquent functionality like relationships, doesn't the repository just become Eloquent?

[](#if-i-expose-all-of-the-eloquent-functionality-like-relationships-doesnt-the-repository-just-become-eloquent)

Yes, that would happen. That's why you shouldn't do that. In the original definition of the repository design pattern a thing called `Specification` was used to help add context and build up the query. In this package I use `Criteria` and provide some basic ones for use.

For example, you could have a `PostRepository::getPosts()` method. If you wanted to get posts for a specific user you'd add a criteria object `$repository->with(new ForUser($user))->getPosts()`, perhaps you only want posts for a given category? `$repository->with(new ForCategory($category))->getPosts()`. Immediately you're writing less code, the process is simpler and easily understandable. These criteria can also be used by anything else that has a relationship with users or categories.

### I'm still not sold

[](#im-still-not-sold)

Well it's not for everyone. Ultimately the choice is yours in whether or not use this package or even this design pattern. All I ask is that you look at objectively and give it a go. If you go in thinking it'll just make things more difficult, you'll find a reason to dislike it.

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

[](#installation)

Super simple and super fun. Run the following command in your terminal.

```
composer require ollieread/laravel-repositories

```

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

[](#configuration)

There is none just yet, but there may be in the future.

Usage
-----

[](#usage)

To use this package, simply create yourself a repository, lets say `App\Repositories\PostRepository` and extend the base repository;

```
Ollieread\Repositories\Repository

```

Next you define your `model()` method to return the FQN for the model this repository represents.

```
protected function model(): string
{
    return Post::class;
}
```

There you go, a fully functioning repository. I like to add in docblocks to hint at the parent return types.

```
