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

ActiveLibrary

pp-spaces/laravel-repository
============================

Repository Design Pattern implementation for Laravel

v1.0.1(6y ago)25.6kMITPHPPHP &gt;=7.2CI failing

Since Mar 26Pushed 6y agoCompare

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

READMEChangelog (1)Dependencies (5)Versions (12)Used By (0)

Laravel Repository Design Pattern
=================================

[](#laravel-repository-design-pattern)

[![Latest Stable Version](https://camo.githubusercontent.com/4351d26c51690b37e0bc4c061958d1d749a0aa2ca3c67a95b5cc55301b3affc0/68747470733a2f2f706f7365722e707567782e6f72672f70702d7370616365732f6c61726176656c2d7265706f7369746f72792f762f737461626c65)](https://packagist.org/packages/pp-spaces/laravel-repository)[![Total Downloads](https://camo.githubusercontent.com/5afa5a607c6d1c87d4490ddaf21fa5aa687b231d88d7b6cc3a983264d276ed86/68747470733a2f2f706f7365722e707567782e6f72672f70702d7370616365732f6c61726176656c2d7265706f7369746f72792f646f776e6c6f616473)](https://packagist.org/packages/pp-spaces/laravel-repository)[![Build Status](https://camo.githubusercontent.com/1c2c28cdbc46f5c61cee79ae97135a7b057387f5b61a36ea17e34a5927f82dea/68747470733a2f2f7472617669732d63692e6f72672f70702d7370616365732f6c61726176656c2d7265706f7369746f72792e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/pp-spaces/laravel-repository)[![License](https://camo.githubusercontent.com/7bba4f84e40bfe13dcd71046e40137e24f9f09fc7354cbcdc8aa5906229c44fe/68747470733a2f2f706f7365722e707567782e6f72672f70702d7370616365732f6c61726176656c2d7265706f7369746f72792f6c6963656e7365)](https://packagist.org/packages/pp-spaces/laravel-repository)

Contents
--------

[](#contents)

- [What is the Repository Design Pattern?](#what-is-the-repository-design-pattern)
- [Usage](#usage)
    - [Installation](#installation)
    - [Make a repository](#make-a-repository)
    - [Use Case](#use-case)
    - [Help](#help)

Upgrade Notice
--------------

[](#upgrade-notice)

> NOTE: The reason why I modified how the `Repository` is being created because I want the `Repository` to use `Route Model Binding` for faster data query.

- [How to upgrade?](#how-to-upgrade)

What is the Repository Design Pattern
-------------------------------------

[](#what-is-the-repository-design-pattern)

[![Repository Design Pattern](assets/repository_pattern.png)](assets/repository_pattern.png)

To put it simply, it is an implementation of a brokering layer between the application and a data source. Neither party needs to be be aware of the other to perform their respective jobs which allows us to have a decoupled architecture which in turn helps in the scaling of the application in the big leagues without having hard dependencies.

Is it the magic bullet
----------------------

[](#is-it-the-magic-bullet)

Well, no it is not. Like every design pattern it has its ups and downs, pros and cons.

### Pros:

[](#pros)

- Separation of concerns; the application need not know about or track any or all data sources.
- Allows easy unit testing as the repositories are bound to interfaces which are injected into classes at run time.
- DRY (Dont Repeat Yourself) design, the code to query and fetch data from data source(s) is not repeated.

### Cons:

[](#cons)

- Adds another layer of abstraction which adds a certain level of complexity making it an overkill for small applications.

Source
------

[](#source)

- [Repository Design Pattern Demystified](https://www.sitepoint.com/repository-design-pattern-demystified/)
- [Use the Repository Design pattern in a Laravel application](https://medium.com/employbl/use-the-repository-design-pattern-in-a-laravel-application-13f0b46a3dce)

Usage
=====

[](#usage)

This package provide a command-line interface for you to create repository in your **Laravel** application.

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

[](#installation)

Require `pp-spaces/laravel-repository` package to your laravel installation

```
composer require pp-spaces/laravel-repository
```

Make a repository
-----------------

[](#make-a-repository)

Run the following command to generate repository:

```
php artisan make:repository UserRepository
```

To make model repository simply run:

```
php artisan make:repository UserRepository --model=User
```

Use Case
--------

[](#use-case)

```
   +-------------+                                +-------------+       +-------------+
   |             |                                |             |       |             |
   |             |---------------------------------             ---------             |
   |             |      +-------------------+     |             |       |             |
   | Controllers |      |      Persist      |     | Repository  |       |   Models    |
   |             |      |   Database Query  |     |             |       |             |
   |             |      +-------------------+     |             |       |             |
   |             ---------------------------------|             ---------             |
   |             |                                |             |       |             |
   +-------------+                                +-------------+       +-------------+

```

### How to use Repository

[](#how-to-use-repository)

Create your repository, e.g. `UserRepository` for `User` model:

```
php artisan make:repository UserRepository --model=User
```

Update `UserRepository` logic:

```
namespace App\Http\Repositories;

use PPSpaces\Repositories\Repository;

class UserRepository extends Repository {

    /**
     * The user model instance.
     *
     * @var \App\User
     */
    protected $model = "App\User";

    /**
     * Scope a query for the model before executing
     *
     * @param \Illuminate\Database\Query\Builder $query
     * @return void
     */
    public function before($query) {
        $query->role('staff');
    }

    /**
     * Get all of the models from the database.
     *
     * @param  array|mixed  $columns
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function get($columns = ['*']) {
        $users = $this->repository
                    ->active()
                    ->orderBy('updated_at', 'DESC')
                    ->get();

        return $users;
    }
}
```

> NOTE: Check `PPSpaces\Repositories\Model` for available methods that you may override. Keep in mind that you still have access to all Model instance that you've created. The `$this->user` is the instance of your `\App\User` model.

Within your `UserController` assume you have a resource controller created. Inject the `UserRepository` to the contoller. Now you can access the repository in your controller method:

```
use App\Http\Repositories\UserRepository;

class UserController extends Controller
{
    protected $users;

    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    public function index()
    {
        return $this->users->get();
    }
}
```

Or alternatively, you may use **Route Model Binding** on the controller actions whose `type-hinted` variable names match a route segment name.

> Read more about [Route Model Binding](https://laravel.com/docs/master/routing#route-model-binding) here

```
public function index(UserRepository $user)
{
    return $user->get();
}

public function show(UserRepository $user)
{
    // Authorizing the repository model
    // Check https://laravel.com/docs/master/authorization
    $this->authorize('view', $user->model());

    // This $user will resolved by the id provided by the router
    // e.g. /api/user/1
    // $user will be the result of $user->id === 1
    return $user;
}
```

How to upgrade?
---------------

[](#how-to-upgrade)

> Upgrade from `v0.0.9` or earilier to `v1.0.0`

### What you need to do

[](#what-you-need-to-do)

```
namespace App\Http\Repositories;

- use App\User;

- use PPSpaces\Repositories\Model as Repository;
+ use PPSpaces\Repositories\Repository;

class UserRepository extends Repository {

+    /**
+     * The user model instance.
+     *
+     * @var \App\User
+     */
+    protected $model = "App\User";

-     protected $user;

-    public function __construct(User $user) {
-        $this->user = $user;
-    }

    public function index()
    {
         // `$this->users->all()` will always resolved the same result as `$this->users->get()`
-        return $this->users->all();
+        return $this->users->get();
    }

}
```

Help
----

[](#help)

```
Description:
  Create a new repository class

Usage:
  make:repository [options] [--]

Arguments:
  name                  The name of the class

Options:
  -m, --model[=MODEL]   Generate a repository for the given model.
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity61

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~27 days

Recently: every ~68 days

Total

11

Last Release

2330d ago

Major Versions

v0.0.9 → v1.0.02019-04-11

PHP version history (2 changes)v0.0.1PHP &gt;=7.0

v1.0.1PHP &gt;=7.2

### Community

Maintainers

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

---

Top Contributors

[![socheatsok78](https://avatars.githubusercontent.com/u/4363857?v=4)](https://github.com/socheatsok78 "socheatsok78 (121 commits)")

---

Tags

laravellibraryphprepositoryrepository-patternlaravelrepository patternrepositorylaravel-repositorypp-spaces

### Embed Badge

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

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

###  Alternatives

[torann/laravel-repository

Base repository implementation for Laravel

88497.8k2](/packages/torann-laravel-repository)[adobrovolsky97/laravel-repository-service-pattern

Laravel 5|6|7|8|9|10 - Repository - Service Pattern

275.4k](/packages/adobrovolsky97-laravel-repository-service-pattern)[mahabub/laravel-crud-and-repository-generator

laravel crud generator with repository pattern

111.7k](/packages/mahabub-laravel-crud-and-repository-generator)

PHPackages © 2026

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