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

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

eveleen/repository
==================

Repository for Laravel 5

v2.6.0(3y ago)05MITPHPPHP &gt;=5.5.9

Since Jun 5Pushed 3y agoCompare

[ Source](https://github.com/we820731/laravel-repository)[ Packagist](https://packagist.org/packages/eveleen/repository)[ RSS](/packages/eveleen-repository/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (10)Versions (34)Used By (0)

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

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

[![StyleCI](https://camo.githubusercontent.com/e443784f374ed60bc457d5dcc8d33853b2e8e70f5d0d3a7bb7bbaab498ff0821/68747470733a2f2f7374796c6563692e696f2f7265706f732f36303333323139342f736869656c643f7374796c653d666c6174)](https://styleci.io/repos/60332194)[![Build Status](https://camo.githubusercontent.com/79ea37872e3c4649f71574747c909675a844d9cf53d78fb883b77f562f1d1b2e/68747470733a2f2f7472617669732d63692e6f72672f7265636361303132302f6c61726176656c2d7265706f7369746f72792e737667)](https://travis-ci.org/recca0120/laravel-repository)[![Total Downloads](https://camo.githubusercontent.com/ce4167978cfddfe039e8e8dfa294c86a85e241d43233ae854b7f11f7f1737a06/68747470733a2f2f706f7365722e707567782e6f72672f7265636361303132302f7265706f7369746f72792f642f746f74616c2e737667)](https://packagist.org/packages/recca0120/repository)[![Latest Stable Version](https://camo.githubusercontent.com/ac72fee0f732104c4779b0f905502d21d12d9f2f47048d24c7e32d0c0e7ef5b9/68747470733a2f2f706f7365722e707567782e6f72672f7265636361303132302f7265706f7369746f72792f762f737461626c652e737667)](https://packagist.org/packages/recca0120/repository)[![Latest Unstable Version](https://camo.githubusercontent.com/24d88f970346a24e62ec597cdabe87fb38ec7f21c8c345dd25d379e90418a3af/68747470733a2f2f706f7365722e707567782e6f72672f7265636361303132302f7265706f7369746f72792f762f756e737461626c652e737667)](https://packagist.org/packages/recca0120/repository)[![License](https://camo.githubusercontent.com/c23cfae4376749d3f4416a8c45403fe197c99ad9987c00ebd0e1bb17f0cd9a35/68747470733a2f2f706f7365722e707567782e6f72672f7265636361303132302f7265706f7369746f72792f6c6963656e73652e737667)](https://packagist.org/packages/recca0120/repository)[![Monthly Downloads](https://camo.githubusercontent.com/91accaebaa63ebd6cbd93d3e1f53febfa3b0b05437031abb46dbea034ffc5bd7/68747470733a2f2f706f7365722e707567782e6f72672f7265636361303132302f7265706f7369746f72792f642f6d6f6e74686c79)](https://packagist.org/packages/recca0120/repository)[![Daily Downloads](https://camo.githubusercontent.com/524bddd584e4379bc6e9201e1af1b495b9c2130cfc9dcd3a26a6f86fa3db3057/68747470733a2f2f706f7365722e707567782e6f72672f7265636361303132302f7265706f7369746f72792f642f6461696c79)](https://packagist.org/packages/recca0120/repository)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/716c361440a8d20540d5b87868cc377e341e73f7e8e1107b9dc9f795640aa0b9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7265636361303132302f6c61726176656c2d7265706f7369746f72792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/recca0120/laravel-repository/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/7671d51418c3dd9e722fbb745e5cbe6b92338f24c3f2617d90f348735439f23f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7265636361303132302f6c61726176656c2d7265706f7369746f72792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/recca0120/laravel-repository/?branch=master)

Install
-------

[](#install)

To get the latest version of Laravel Exceptions, simply require the project using [Composer](https://getcomposer.org):

```
composer require recca0120/repository
```

Instead, you may of course manually update your require block and run `composer update` if you so choose:

```
{
    "require": {
        "recca0120/repository": "~2.0.0"
    }
}
```

Methods
-------

[](#methods)

### Recca0120\\Repository\\EloquentRepository

[](#recca0120repositoryeloquentrepository)

- find($id, $columns = \['\*'\]);
- findMany($ids, $columns = \['\*'\]);
- findOrFail($id, $columns = \['\*'\]);
- findOrNew($id, $columns = \['\*'\]);
- firstOrNew(array $attributes, array $values = \[\]);
- firstOrCreate(array $attributes, array $values = \[\]);
- updateOrCreate(array $attributes, array $values = \[\]);
- firstOrFail($criteria = \[\], $columns = \['\*'\]);
- create($attributes);
- forceCreate($attributes);
- update($id, $attributes);
- forceUpdate($id, $attributes);
- delete($id);
- forceDelete($id);
- newInstance($attributes = \[\], $exists = false);
- get($criteria = \[\], $columns = \['\*'\]);
- chunk($criteria, $count, callable $callback);
- each($criteria, callable $callback, $count = 1000);
- first($criteria = \[\], $columns = \['\*'\]);
- paginate($criteria = \[\], $perPage = null, $columns = \['\*'\], $pageName = 'page', $page = null);
- simplePaginate($criteria = \[\], $perPage = null, $columns = \['\*'\], $pageName = 'page', $page = null);
- count($criteria = \[\], $columns = '\*');
- min($criteria, $column);
- max($criteria, $column);
- sum($criteria, $column);
- avg($criteria, $column);
- average($criteria, $column);
- matching($criteria);
- getQuery($criteria = \[\]);
- getModel();
- newQuery();

### Recca0120\\Repository\\Criteria

[](#recca0120repositorycriteria)

- static create()
- static expr($value)
- static raw($value)
- select($columns = \['\*'\])
- selectRaw($expression, array $bindings = \[\])
- selectSub($query, $as)
- addSelect($column)
- distinct()
- from($table)
- join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
- joinWhere($table, $first, $operator, $second, $type = 'inner')
- leftJoin($table, $first, $operator = null, $second = null)
- leftJoinWhere($table, $first, $operator, $second)
- rightJoin($table, $first, $operator = null, $second = null)
- rightJoinWhere($table, $first, $operator, $second)
- crossJoin($table, $first = null, $operator = null, $second = null)
- mergeWheres($wheres, $bindings)
- tap($callback)
- where($column, $operator = null, $value = null, $boolean = 'and')
- orWhere($column, $operator = null, $value = null)
- whereColumn($first, $operator = null, $second = null, $boolean = 'and')
- orWhereColumn($first, $operator = null, $second = null)
- whereRaw($sql, $bindings = \[\], $boolean = 'and')
- orWhereRaw($sql, array $bindings = \[\])
- whereIn($column, $values, $boolean = 'and', $not = false)
- orWhereIn($column, $values)
- whereNotIn($column, $values, $boolean = 'and')
- orWhereNotIn($column, $values)
- whereNull($column, $boolean = 'and', $not = false)
- orWhereNull($column)
- whereNotNull($column, $boolean = 'and')
- whereBetween($column, array $values, $boolean = 'and', $not = false)
- orWhereBetween($column, array $values)
- whereNotBetween($column, array $values, $boolean = 'and')
- orWhereNotBetween($column, array $values)
- orWhereNotNull($column)
- whereDate($column, $operator, $value = null, $boolean = 'and')
- orWhereDate($column, $operator, $value)
- whereTime($column, $operator, $value, $boolean = 'and')
- orWhereTime($column, $operator, $value)
- whereDay($column, $operator, $value = null, $boolean = 'and')
- whereMonth($column, $operator, $value = null, $boolean = 'and')
- whereYear($column, $operator, $value = null, $boolean = 'and')
- whereNested(Closure $callback, $boolean = 'and')
- addNestedWhereQuery($query, $boolean = 'and')
- whereExists(Closure $callback, $boolean = 'and', $not = false)
- orWhereExists(Closure $callback, $not = false)
- whereNotExists(Closure $callback, $boolean = 'and')
- orWhereNotExists(Closure $callback)
- addWhereExistsQuery(Builder $query, $boolean = 'and', $not = false)
- dynamicWhere($method, $parameters)
- groupBy()
- having($column, $operator = null, $value = null, $boolean = 'and')
- orHaving($column, $operator = null, $value = null)
- havingRaw($sql, array $bindings = \[\], $boolean = 'and')
- orHavingRaw($sql, array $bindings = \[\])
- orderBy($column, $direction = 'asc')
- orderByDesc($column)
- latest($column = 'created\_at')
- oldest($column = 'created\_at')
- inRandomOrder($seed = '')
- orderByRaw($sql, $bindings = \[\])
- skip($value)
- offset($value)
- take($value)
- limit($value)
- forPage($page, $perPage = 15)
- forPageAfterId($perPage = 15, $lastId = 0, $column = 'id')
- union($query, $all = false)
- unionAll($query)
- lock($value = true)
- lockForUpdate()
- sharedLock()
- when($value, $callback, $default = null)
- unless($value, $callback, $default = null)
- whereKey($id)
- whereKeyNot($id)
- with($relations)
- without($relations)
- setQuery($query)
- setModel(Model $model)
- has($relation, $operator = '&gt;=', $count = 1, $boolean = 'and', Closure $callback = null)
- orHas($relation, $operator = '&gt;=', $count = 1)
- doesntHave($relation, $boolean = 'and', Closure $callback = null)
- whereHas($relation, Closure $callback = null, $operator = '&gt;=', $count = 1)
- orWhereHas($relation, Closure $callback = null, $operator = '&gt;=', $count = 1)
- whereDoesntHave($relation, Closure $callback = null)
- withCount($relations)
- mergeConstraintsFrom(Builder $from)
- withTrashed()
- withoutTrashed()
- onlyTrashed()

Usage
-----

[](#usage)

### Eloquent

[](#eloquent)

#### Create a Model

[](#create-a-model)

Create your model normally, but it is important to define the attributes that can be filled from the input form data.

```
namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'title',
        'author',
     ];
}
```

### Create a Contract

[](#create-a-contract)

```
namespace App\Repositories\Contracts;

interface PostRepository
{

}
```

#### Create a Repository

[](#create-a-repository)

```
namespace App\Repositories;

use App\Repositories\Contracts\PostRepository as PostRepositoryContract;
use App\Post;
use Recca0120\Repository\EloquentRepository;

class PostRepository extends EloquentRepository implements PostRepositoryContract
{
    public function __construct(Post $model)
    {
        $this->model = $model;
    }
}
```

### Bind

[](#bind)

```
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Repositories\Contracts\PostRepository as PostRepositoryContract;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(PostRepositoryContract::class, PostRepository::class);
    }
}
```

#### Controller

[](#controller)

```
namespace App\Http\Controllers;

use App\Repositories\Contracts\PostRepository;

class PostsController extends Controller
{
    protected $repository;

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

Methods
-------

[](#methods-1)

Find all results in Repository

```
$posts = $this->repository->get();
```

Find all results in Repository with pagination

```
$posts = $this->repository->paginate();
```

Count results in Repository

```
$posts = $this->repository->count();
```

Create new entry in Repository

```
$post = $this->repository->create(request()->all());
```

Update entry in Repository

```
$post = $this->repository->update($id, request()->all());
```

Delete entry in Repository

```
$this->repository->delete($id);
```

New instance

```
$post = $this->repository->newInstance([
    'author' => 'author'
]);
```

Return Model With Conditions

```
$model = $this->repository->matching(Criteria::create()->where('title', '=', 'title'));

```

Find result by id

```
$post = $this->repository->find($id);
```

### Find by conditions

[](#find-by-conditions)

#### Using the Criteria

[](#using-the-criteria)

Criteria is support all of Eloquent functions

##### Single Criteria

[](#single-criteria)

```
use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->select('*')
    ->where('author', '=', 'author')
    ->orWhere('title', '=', 'title')
    ->orderBy('author', 'asc');

$this->repository->get($criteria);
$this->repository->paginate($criteria);
```

#### Multiple Criteria

[](#multiple-criteria)

```
use Recca0120\Repository\Criteria;

$criteria = [];

$criteria[] = Criteria::create()
    ->orderBy('author', 'asc');

$criteria[] = Criteria::create()
    ->where('author', '=', 'author')
    ->orWhere('title', '=', 'title');

$this->repository->get($criteria);
// $this->repository->paginate($criteria);
```

##### With

[](#with)

```
use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->with('author', function($criteria) {
        $criteria->where('author', 'author');
    });

$this->repository->get($criteria);
// $this->repository->paginate($criteria);
```

#### Join

[](#join)

```
use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->join('author', function ($criteria) {
        $criteria->on('posts.author_id', '=', 'author.id');
    });

$this->repository->get($criteria);
// $this->repository->paginate($criteria);
```

#### Expression

[](#expression)

```
use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->where('created_at', '
