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

Abandoned → [https://github.com/andersao/l5-repository](/?search=https%3A%2F%2Fgithub.com%2Fandersao%2Fl5-repository)Library[Database &amp; ORM](/categories/database)

guilhermegonzaga/repository
===========================

Repositories for Laravel 5 which is used to abstract the database layer.

1.0.10(10y ago)1053MITPHPPHP &gt;=5.5.9

Since Jan 7Pushed 10y ago1 watchersCompare

[ Source](https://github.com/guilhermegonzaga/repository)[ Packagist](https://packagist.org/packages/guilhermegonzaga/repository)[ RSS](/packages/guilhermegonzaga-repository/feed)WikiDiscussions master Synced today

READMEChangelog (7)Dependencies (2)Versions (10)Used By (0)

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

[](#laravel-repositories)

[![Latest Stable Version](https://camo.githubusercontent.com/ce6a7091ece7330ebb579bbd112f94b47b64f7f65813cdae985a13e17bfd1959/68747470733a2f2f706f7365722e707567782e6f72672f6775696c6865726d65676f6e7a6167612f7265706f7369746f72792f762f737461626c65)](https://packagist.org/packages/guilhermegonzaga/repository) [![Total Downloads](https://camo.githubusercontent.com/f78cef2fc21752aac281cb76b3ec5dc6075c1a8183bd34fc5ed98bd0c9c51897/68747470733a2f2f706f7365722e707567782e6f72672f6775696c6865726d65676f6e7a6167612f7265706f7369746f72792f646f776e6c6f616473)](https://packagist.org/packages/guilhermegonzaga/repository) [![Latest Unstable Version](https://camo.githubusercontent.com/93972e8ef4aaf4f9e750efa9690f2da2b907f493454e0d83e5a3bca399235dcb/68747470733a2f2f706f7365722e707567782e6f72672f6775696c6865726d65676f6e7a6167612f7265706f7369746f72792f762f756e737461626c65)](https://packagist.org/packages/guilhermegonzaga/repository) [![License](https://camo.githubusercontent.com/8f9c159c5d9de1f526c44d9c98b292b4690690967da0a7cefcdd1afe1edf36ef/68747470733a2f2f706f7365722e707567782e6f72672f6775696c6865726d65676f6e7a6167612f7265706f7369746f72792f6c6963656e7365)](https://packagist.org/packages/guilhermegonzaga/repository)

Repository is a design pattern for Laravel 5 which is used to abstract the database layer.
It makes your application easier to be maintained.

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

[](#installation)

#### Laravel (5.1 and 5.2)

[](#laravel-51-and-52)

Execute the following command to get the latest version of the package:

```
composer require guilhermegonzaga/repository

```

Methods
-------

[](#methods)

```
public function first($columns = ['*'], $fail = true);
public function find($id, $columns = ['*'], $fail = true);
public function findBy($attribute, $value);
public function where(array $where, $boolean = 'and');
public function create(array $data, $force = true);
public function update(array $data, $id = null, $force = true);
public function delete($id = null);
public function paginate($limit = 15, $columns = ['*'], $pageName = 'page');
public function exists();
public function random($qtd = 15);
public function scopes(Closure $scope, $boolean = 'and');
public function criteria($class, array $args = []);
public function with($relations);
public function withBoot();
public function withoutBoot();
public function all($columns = ['*']);
public function get($columns = ['*']);
```

Usage
-----

[](#usage)

Create your repository class.
Note that your repository class must extend `GuilhermeGonzaga\Repository\Eloquent\Repository` and implement `model()` method.

```
namespace App\Repositories;

use GuilhermeGonzaga\Repository\Eloquent\Repository;

class CategoryRepository extends Repository
{
    public function model()
    {
        return \App\Category::class;
    }

    // Optional method, global rules
    public function boot()
    {
        $this->findBy('active', true);
        $this->orderBy('created_at', 'desc');
    }

    // Other methods
}
```

By implementing `model()` method you telling repository what model class you want to use.
Create your model:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [
        'name',
        'parent_id',
        ...
    ];

    ...
}
```

Finally, use the repository in the controller:

```
use App\Repositories\CategoryRepository;

class CategoriesController extends Controller
{
    protected $repository;

    public function __construct(CategoryRepository $repository)
    {
        $this->repository = $repository;
    }

    ...
}
```

Usage methods
-------------

[](#usage-methods)

Find all results:

```
$results = $this->repository->all();
```

Find first result:

```
$result = $this->repository->where([['abc', '!=', 'def']])->first(); // Fire ModelNotFoundException (firstOrFail)
$result = $this->repository->findBy('abc', 'def')->first(['*'], false); // Not fire ModelNotFoundException (first)
```

Find all results with pagination:

```
$results = $this->repository->paginate();
$results = $this->repository->paginate(10);
```

Find result by id:

```
$result = $this->repository->find($id); // Fire ModelNotFoundException (findOrFail)
$result = $this->repository->find($id, ['*'], false); // Not fire ModelNotFoundException (find)
```

Loading the model relationships:

```
$result = $this->repository->with('relationName')->find($id);
$result = $this->repository->with(['relation1', 'relation2'])->find($id);
```

Find result by multiple fields:

```
$results = $this->repository->where([

    //Default condition (=)
    'user_id' => '10',

    //Custom condition
    ['price', '>=', '9.90']

])->get();
```

Find using custom scope (nested):

```
$results = $this->repository->scopes(function ($query) {
    $query->whereDate('birth_date', '=', Carbon::now()->toDateString());
    $query->whereActive(true);
})->get();
```

Get random results:

```
$results = $this->repository->whereFeatured(true)->scopes(function ($query) { ... }, 'or')->random()->get();
$results = $this->repository->whereNull('discount')->random(10)->get();
```

Enable/disable `boot()` method in repository:

```
$results = $this->repository->withBoot()->all();
$results = $this->repository->withoutBoot()->all();
```

Create new entry:

```
$result = $this->repository->create(Input::all()); // without $fillable
$result = $this->repository->create(Input::all(), false); // with $fillable
```

Update entry:

```
$result = $this->repository->update(Input::all(), $id); // without $fillable
$result = $this->repository->update(Input::all(), $id, false); // with $fillable
$result = $this->repository->whereFieldName('test')->update(Input::all()); // update first result
```

Delete entry:

```
$this->repository->delete($id);
$this->repository->delete([1, 2, 3]);
$this->repository->whereActive(false)->where(['payment_failed' => true], 'or')->delete();
```

Criteria
--------

[](#criteria)

This package makes it very easy to work with scopes/criteria.
Create classes to abstract these rules and make them reusable:

```
namespace App\Repositories\Criteria;

use GuilhermeGonzaga\Repository\Contracts\RepositoryContract as Repository;
use GuilhermeGonzaga\Repository\Criteria\Criteria;

class PopularProducts extends Criteria
{
    public function apply(Repository $repository)
    {
        $repository->where([
            ['purchases', '>', '50']
        ]);
    }
}
```

Receiving arguments:

```
namespace App\Repositories\Criteria;

use GuilhermeGonzaga\Repository\Contracts\RepositoryContract as Repository;
use GuilhermeGonzaga\Repository\Criteria\Criteria;

class ProductsByCategory extends Criteria
{
    protected $category;

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

    public function apply(Repository $repository)
    {
        $repository->findBy('category_id', $this->category);
    }
}
```

Use the criteria in the controller:

```
use App\Repositories\ProductRepository;
use App\Repositories\Criteria\PopularProducts;
use App\Repositories\Criteria\ProductsByCategory;
use App\Repositories\Criteria\ProductsByCategories;

class ProductsController extends Controller
{
    protected $repository;

    public function __construct(ProductRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index()
    {
        $this->repository->criteria(PopularProducts::class)->get();
    }

    public function showByCategory($category)
    {
        $this->repository->criteria(ProductsByCategory::class, [$category])->get();
    }

    public function showByCategories($category1, $category2)
    {
        $this->repository->criteria(ProductsByCategories::class, [
            $category1,
            $category2,
            $category3
        ])->get();
    }
}
```

Other methods
-------------

[](#other-methods)

In addition to the methods that are available in this package, you can call any method in default of Eloquent Builder. When you call a method that does not exist in this package, it's automatically called on Eloquent Builder.

Ex:

```
$results = $this->repository->orderBy('created_at', 'desc')->get();
$results = $this->repository->whereIn('category_id', [2, 4, 6])->get();
$results = $this->repository->whereBetween('votes', [10, 100])->get();
$results = $this->repository->whereFieldName('test')->get();
```

Credits
-------

[](#credits)

This package is largely inspired by [this](https://github.com/andersao/l5-repository) great package by @andersao.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity64

Established project with proven stability

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

Recently: every ~21 days

Total

9

Last Release

3731d ago

Major Versions

1.0.10 → 2.0.x-dev2016-04-13

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/12283475?v=4)[Guilherme Gonzaga](/maintainers/guilhermegonzaga)[@guilhermegonzaga](https://github.com/guilhermegonzaga)

---

Tags

laraveldatabasemodeleloquentrepositoryrepositories

### Embed Badge

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

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

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M90](/packages/mongodb-laravel-mongodb)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

592452.8k2](/packages/spiritix-lada-cache)[czim/laravel-repository

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

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

PHPackages © 2026

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