PHPackages                             awema-pl/module-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. awema-pl/module-repository

ActiveLibrary

awema-pl/module-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.0.10(5y ago)06313MITPHP

Since Nov 23Pushed 5y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (12)Used By (13)

 [ ![Awema.pl logo](/assets/awema-pl/repository/promo/Logo_sign_color.svg) ](https://www.awema.pl/?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/92d02d29636823d31fb1a6a54e250ca5ee25631df0ad23a21f98d71621669522/68747470733a2f2f7265706f2e6177656d612e706c2f344742574f2f6177656d612d706c2f6d6f64756c652d7265706f7369746f72792f6261646765732f6d61737465722f636f7665726167652e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![Last version](https://camo.githubusercontent.com/e2f7e728bc912a8b316337361857f39ccc836b7b9f81f0e40cf42e6983359caf/68747470733a2f2f7777772e6177656d612e706c2f344742574f2f6177656d612d706c2f6d6f64756c652d7265706f7369746f72792f76657273696f6e2e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![Build status](https://camo.githubusercontent.com/eb0cc3d594653de355a5c9b983784053519b5b69b4e978e59be789cca55bb137/68747470733a2f2f7265706f2e6177656d612e706c2f344742574f2f6177656d612d706c2f6d6f64756c652d7265706f7369746f72792f6261646765732f6d61737465722f6275696c642e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![Downloads](https://camo.githubusercontent.com/c1d52ab3a5b7535af73352ad0b0556ed8487b833124ab8b42932eac2bee25ebd/68747470733a2f2f7777772e6177656d612e706c2f344742574f2f6177656d612d706c2f6d6f64756c652d7265706f7369746f72792f646f776e6c6f6164732e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![License](https://camo.githubusercontent.com/4ad3edcae195c47f76b5404710acdc79258b9595f5922c06d3ad56fec0aced19/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6177656d612d706c2f6d6f64756c652d7265706f7369746f72792e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![CDN Ready](https://camo.githubusercontent.com/c92d659da7bd968f2f85d3ae092bf20b32602a451de102e3c6ff9bf4f4425f89/68747470733a2f2f7777772e6177656d612e706c2f344742574f2f6177656d612d706c2f6d6f64756c652d7265706f7369746f72792f7374617475732e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![laravel](/assets/awema-pl/repository/badges/laravel.svg) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![Last commit](https://camo.githubusercontent.com/baae9d13ccce628a633855ead2ce544f787792a4bbef3f5478b4120ed25bd719/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f6177656d612d706c2f6d6f64756c652d7265706f7369746f72792e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![Analytics](https://camo.githubusercontent.com/7f16872bbe19ba5ed610089882d3804340fe219411565da6328e4d52227e3077/68747470733a2f2f67612d626561636f6e2e61707073706f742e636f6d2f55412d3133343433313633362d312f6177656d612d706c2f6d6f64756c652d7265706f7369746f7279) ](https://github.com/awema-pl/awema-pl) [ ![Hosted by Package Kit](https://camo.githubusercontent.com/49e48d1673493f772d8b4552bbb34a98d109710a26e9a429e7255319b728233c/68747470733a2f2f7777772e6177656d612e706c2f6261646765732f686f737465642e737667) ](https://www.awema.pl/?utm_source=github&utm_medium=shields) [ ![Patreon](/assets/awema-pl/repository/badges/patreon.svg) ](https://www.patreon.com/join/awemadotio)

 [![Repository Laravel](/assets/awema-pl/repository/github/repository-cover.png)](/assets/awema-pl/repository/github/repository-cover.png)

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 awema-pl/module-repository
```

The package will automatically register itself.

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

[](#configuration)

First publish config:

```
php artisan vendor:publish --provider="AwemaPL\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 `AwemaPL\Repository\Eloquent\BaseRepository` and provide `entity()` method to return full model class name:

```
namespace App;

use AwemaPL\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', '
