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

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

awes-io/repository
==================

Implementation of repository pattern for Laravel. The package allows out-of-the-box filtering of data based on parameters in the &lt;b&gt;request&lt;/b&gt;, and also allows you to quickly integrate the list filters and custom criteria.

v1.1.8(4y ago)171105.4k22[19 issues](https://github.com/awes-io/repository/issues)[2 PRs](https://github.com/awes-io/repository/pulls)5MITPHPCI failing

Since Jan 29Pushed 3y ago11 watchersCompare

[ Source](https://github.com/awes-io/repository)[ Packagist](https://packagist.org/packages/awes-io/repository)[ Docs](https://github.com/awes-io/repository)[ Fund](https://opencollective.com/awesdotio)[ Patreon](https://www.patreon.com/awesdotio)[ RSS](/packages/awes-io-repository/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (5)Versions (25)Used By (5)

 [ ![Awes.io logo](https://camo.githubusercontent.com/3411caf1ea53cc7f92a106e51ad617072eed81a7d52da1aca5eea4f96b6d001b/68747470733a2f2f7374617469632e617765732e696f2f70726f6d6f2f4c6f676f5f7369676e5f636f6c6f722e737667) ](https://www.awes.io/?utm_source=github&utm_medium=repository)

Repository
==========

[](#repository)

Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.

 [ ![Coverage report](https://camo.githubusercontent.com/d2feca6233c9d2add0acd69780899eb63beddb4d9daebef9d96479410002a295/68747470733a2f2f7265706f2e706b676b69742e636f6d2f344742574f2f617765732d696f2f7265706f7369746f72792f6261646765732f6d61737465722f636f7665726167652e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![Last version](https://camo.githubusercontent.com/442d6e02e6aa6545a201b8dd6e0188b4c1c6f71455ad89377b312bbb8bcbebab/68747470733a2f2f7777772e706b676b69742e636f6d2f344742574f2f617765732d696f2f7265706f7369746f72792f76657273696f6e2e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![Build status](https://camo.githubusercontent.com/7bfd5ba812793c06377de60ae1cb5c6f2b31de04d8852cb8798452a8facd8cc8/68747470733a2f2f7265706f2e706b676b69742e636f6d2f344742574f2f617765732d696f2f7265706f7369746f72792f6261646765732f6d61737465722f6275696c642e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![Downloads](https://camo.githubusercontent.com/6c752ff94848e9ddb96f968c1313c24a832f4c2d0dee3013cba7ef7f2d8eca14/68747470733a2f2f7777772e706b676b69742e636f6d2f344742574f2f617765732d696f2f7265706f7369746f72792f646f776e6c6f6164732e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![License](https://camo.githubusercontent.com/23cc8d40cd3524cb14b69ec66b5efcf2e80385d44acc4c09f46253055d2c4452/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f617765732d696f2f7265706f7369746f72792e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![CDN Ready](https://camo.githubusercontent.com/a39fddd93a2b317fe29387c1c2b35fe8a448d99d34c3013c2f365c23f1a802a7/68747470733a2f2f7777772e706b676b69742e636f6d2f344742574f2f617765732d696f2f7265706f7369746f72792f7374617475732e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![laravel](https://camo.githubusercontent.com/6c8450706caac3ca057866b48ac77f9927d9b206811b156a7c3f05b8e1252b3b/68747470733a2f2f7374617469632e706b676b69742e636f6d2f6261646765732f6c61726176656c2e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![Last commit](https://camo.githubusercontent.com/199becf4ffd9693d9030a803ea7999aa5a68ee50b64896ac69708c1d956900a3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f617765732d696f2f7265706f7369746f72792e737667) ](https://www.awes.io/?utm_source=github&utm_medium=shields) [ ![Analytics](https://camo.githubusercontent.com/529f348936d27d6e7e109e3a4442b66e39d2ee2fd18224dbaef389668520cb4a/68747470733a2f2f67612d626561636f6e2e61707073706f742e636f6d2f55412d3133343433313633362d312f617765732d696f2f7265706f7369746f7279) ](https://github.com/awes-io/awes-io) [ ![Hosted by Package Kit](https://camo.githubusercontent.com/31a8a2063d3a2b5eae3ae31d0be49d4366bfcd848455e6a6cd39bb7b71cb2b97/68747470733a2f2f7777772e706b676b69742e636f6d2f6261646765732f686f737465642e737667) ](https://www.pkgkit.com/?utm_source=github&utm_medium=shields) [ ![Patreon](https://camo.githubusercontent.com/cc15e6db5bcba81594411e6ec54eb020345f90a5d21b37616523a18112cd41ff/68747470733a2f2f7374617469632e706b676b69742e636f6d2f6261646765732f70617472656f6e2e737667) ](https://www.patreon.com/join/awesdotio)

 [![Repository Laravel](https://camo.githubusercontent.com/cbc2156677d67f651eff3565830a87c3b8577410e468b748a51d956c8fd3f5b4/68747470733a2f2f7374617469632e617765732e696f2f6769746875622f7265706f7369746f72792d636f7665722e706e67)](https://camo.githubusercontent.com/cbc2156677d67f651eff3565830a87c3b8577410e468b748a51d956c8fd3f5b4/68747470733a2f2f7374617469632e617765732e696f2f6769746875622f7265706f7369746f72792d636f7665722e706e67)

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Configuration](#configuration)
- [Overview](#overview)
- [Usage](#usage)
    - [Create a Model](#create-a-model)
    - [Create a Repository](#create-a-repository)
    - [Use built-in methods](#use-built-in-methods)
    - [Create a Criteria](#create-a-criteria)
    - [Scope, Filter, and Order](#scope-filter-and-order)
    - [Artisan Commands](#artisan-commands)
- [Testing](#testing)

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

[](#installation)

Via Composer

```
$ composer require awes-io/repository
```

The package will automatically register itself.

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

[](#configuration)

First publish config:

```
php artisan vendor:publish --provider="AwesIO\Repository\RepositoryServiceProvider" --tag="config"
```

```
// $repository->smartPaginate() related parameters
'smart_paginate' => [
    // name of request parameter to take paginate by value from
    'request_parameter' => 'limit',
    // default paginate by value
    'default_limit' => 15,
    // max paginate by value
    'max_limit' => 100,
]
```

Overview
--------

[](#overview)

##### Package allows you to filter data based on incoming request parameters:

[](#package-allows-you-to-filter-data-based-on-incoming-request-parameters)

```
https://example.com/news?title=Title&custom=value&orderBy=name_desc

```

It will automatically apply built-in constraints onto the query as well as any custom scopes and criteria you need:

```
protected $searchable = [
    // where 'title' equals 'Title'
    'title',
];

protected $scopes = [
    // and custom parameter used in your scope
    'custom' => MyScope::class,
];
```

```
class MyScope extends ScopeAbstract
{
    public function scope($builder, $value, $scope)
    {
        return $builder->where($scope, $value)->orWhere(...);
    }
}
```

Ordering by any field is available:

```
protected $scopes = [
    // orderBy field
    'orderBy' => OrderByScope::class,
];
```

Package can also apply any custom criteria:

```
return $this->news->withCriteria([
    new MyCriteria([
        'category_id' => '1', 'name' => 'Name'
    ])
    ...
])->get();
```

Usage
-----

[](#usage)

### Create a Model

[](#create-a-model)

Create your model:

```
namespace App;

use Illuminate\Database\Eloquent\Model;

class News extends Model
{
    ...
}
```

### Create a Repository

[](#create-a-repository)

Extend it from `AwesIO\Repository\Eloquent\BaseRepository` and provide `entity()` method to return full model class name:

```
namespace App;

use AwesIO\Repository\Eloquent\BaseRepository;

class NewsRepository extends BaseRepository
{
    public function entity()
    {
        return News::class;
    }
}
```

### Use built-in methods

[](#use-built-in-methods)

```
use App\NewsRepository;

class NewsController extends BaseController
{
    protected $news;

    public function __construct(NewsRepository $news)
    {
        $this->news = $news;
    }
    ....
}
```

Execute the query as a "select" statement or get all results:

```
$news = $this->news->get();
```

Execute the query and get the first result:

```
$news = $this->news->first();
```

Find a model by its primary key:

```
$news = $this->news->find(1);
```

Add basic where clauses and execute the query:

```
$news = $this->news->->findWhere([
        // where id equals 1
        'id' => '1',
        // other "where" operations
        ['news_category_id', '
