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

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

crcms/repository
================

Detached model and controller warehouse data provider

2.1.17(6y ago)664.4k↓91.7%125MITPHPPHP &gt;=7.2

Since Jan 17Pushed 6y ago1 watchersCompare

[ Source](https://github.com/crcms/repository)[ Packagist](https://packagist.org/packages/crcms/repository)[ Docs](https://github.com/crcms/repository)[ RSS](/packages/crcms-repository/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (5)Versions (27)Used By (5)

CRCMS Repository
----------------

[](#crcms-repository)

[![Latest Stable Version](https://camo.githubusercontent.com/65e9d395981a471155ca00088fc58d1fafd41c8487da14af714a87f4c6202ed3/68747470733a2f2f706f7365722e707567782e6f72672f6372636d732f7265706f7369746f72792f762f737461626c65)](https://packagist.org/packages/crcms/repository)[![License](https://camo.githubusercontent.com/a5c006a4256fb1758a1e8c10e9f1204cbdf3735b6d0573481936b91866c28b06/68747470733a2f2f706f7365722e707567782e6f72672f6372636d732f7265706f7369746f72792f6c6963656e7365)](https://packagist.org/packages/crcms/repository)[![StyleCI](https://camo.githubusercontent.com/2ab4355666463e7983beb936ee5c516b730958b5d7b3fa501ec5a6aa0d2e06e0/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f37353839383538312f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/75898581)

A specialized data provider layer, based on ORM, only as a data provider, the main role is to separate the coupling before the Controller and Model

Install
-------

[](#install)

You can install the package via composer:

```
composer require crcms/repository
```

Laravel
-------

[](#laravel)

If your version is less than 5.5 please modify `config / app.php`

```
'providers' => [
    CrCms\Repository\RepositoryServiceProvider::class,
]
```

If you'd like to make configuration changes in the configuration file you can pubish it with the following Aritsan command:

```
php artisan vendor:publish --provider="CrCms\Repository\RepositoryServiceProvider"
```

Commands
--------

[](#commands)

```
php artisan make:repository TestRepository --model TestModel
```

```
php artisan make:magic TestMagic
```

Example
-------

[](#example)

### QueryMagic

[](#querymagic)

```
use CrCms\Repository\AbstractMagic;
use CrCms\Repository\Contracts\QueryRelate;

class TestMagic extends AbstractMagic
{
    /**
     * @param QueryRelate $queryRelate
     * @param int $id
     * @return QueryRelate
     */
    protected function byName(QueryRelate $queryRelate, string $name)
    {
        return $queryRelate->where('name', $name);
    }

    /**
     * @param QueryRelate $queryRelate
     * @param string $title
     * @return QueryRelate
     */
    protected function byTitle(QueryRelate $queryRelate, string $title)
    {
        return $queryRelate->where('title', 'like', "%{$title}%");
    }

    /**
     * @param QueryRelate $queryRelate
     * @param int $id
     * @return QueryRelate
     */
    protected function byId(QueryRelate $queryRelate, int $id)
    {
        return $queryRelate->where('id', $id);
    }

    /**
     * @param QueryRelate $queryRelate
     * @param array $sort
     * @return QueryRelate
     */
    protected function bySort(QueryRelate $queryRelate, array $sort)
    {
        return $queryRelate->orderByArray($sort);
    }
}
```

### Repository

[](#repository)

```
class TestRepository extends AbstractRepository
{
    /**
     * @var array
     */
    protected $guard = [
        'id', 'title','other'
    ];

    /**
     * @return TestModel
     */
    public function newModel(): TestModel
    {
        return app(TestModel::class);
    }

    /**
     * @param int $perPage
     * @return LengthAwarePaginator
     */
    public function paginate(AbstractMagic $magic = null, int $perPage = 15): LengthAwarePaginator
    {
        return $this->whenMagic($magic)->where('built_in', 1)->orderBy($this->getModel()->getKeyName(), 'desc')->paginate($perPage);
    }

    /**
     * @param int $name
     * @param int $title
     */
    public function updateName(string $name, string $title)
    {
        $this->where('name', $name)->update(['title' => $title]);
    }

}
```

### Guard Or Scenes

[](#guard-or-scenes)

Usually we need to filter the incoming parameter values when adding or modifying and querying the data, and retain the required parameter values.

Guard and scenes are born for this

```
class TestRepository extends AbstractRepository
{
    /**
     * @var array
     */
    protected $scenes = [
        'create' => ['sort', 'added_at'],
        'modify' => ['sort', 'published_at']
    ];

    /**
     * @var array
     */
    protected $guard = [
        'id', 'title', 'other'
    ];
}

$testRepository->create($data, 'create'); //OR
$testRepository->setCurrentScene('create')->create($data); //OR
$testRepository->setGuard(['sort', 'added_at'])->create($data);
```

```
class TestMagic extends AbstractMagic
{
    /**
     * @var array
     */
    protected $scenes = [
        'frontend' => ['name'],
        'backend' => ['title']
    ];

    /**
     * @var array
     */
    protected $guard = [
        'title',
    ];

    /**
     * @param QueryRelate $queryRelate
     * @param int $id
     * @return QueryRelate
     */
    protected function byName(QueryRelate $queryRelate, string $name)
    {
        return $queryRelate->where('name', $name);
    }

    /**
     * @param QueryRelate $queryRelate
     * @param string $title
     * @return QueryRelate
     */
    protected function byTitle(QueryRelate $queryRelate, string $title)
    {
        return $queryRelate->where('title', 'like', "%{$title}%");
    }
}

$testRepository->magic(new TestMagic($data, 'frontend'))->paginate(); //OR
$testRepository->magic((new TestMagic($data))->setCurrentScene('frontend'))->paginate(); //OR
$testRepository->magic((new TestMagic($data))->setGuard(['title']))->paginate(); //OR->create($data);
```

**Note: when guard and scenes are both present, scenes has a higher priority. If scenes is empty, it will use guard.**

### Listener

[](#listener)

```
TestRepository::observer(TestListener::class);

TestListener {

    public function creating(TestRepository $repository, array $data)
    {
		//append the value to be written
		$repository->addData('append_data','value');

		//rewrite all values written
		$repository->setData(['key'=>'value']);
    }

    public function created(TestRepository $repository, TestModel $model)
    {
    }

    public function updating(TestRepository $repository, array $data)
    {
    }

    public function updated(TestRepository $repository, TestModel $model)
    {
    }

    public function deleting(TestRepository $repository, array $ids)
    {
    }

    public function deleted(TestRepository $repository, Collection $models)
    {
    }
}
```

### Cache

[](#cache)

```
class TestRepository {

    public function do(User $user)
    {
        return $this->byIntId($user->id);
    }
}

$repository = new TestRepository;
```

#### store

[](#store)

```
$repository->cache()->do(new User);
```

#### forget

[](#forget)

```
$repository->cache()->forget('do')
```

#### flush

[](#flush)

```
$repository->cache()->flush()
```

### Repository Methods

[](#repository-methods)

```
public function all(): Collection;
```

```
public function get(): Collection;
```

```
public function pluck(string $column, string $key = ''): Collection;
```

```
public function max(string $column): int;
```

```
public function count(string $column = '*'): int;
```

```
public function avg($column): int;
```

```
public function sum(string $column): int;
```

```
public function chunk(int $limit, callable $callback): bool;
```

```
public function valueOfString(string $key, string $default = ''): string;
```

```
public function valueOfInt(string $key, int $default = 0): int;
```

```
public function increment(string $column, int $amount = 1, array $extra = []): int;
```

```
public function decrement(string $column, int $amount = 1, array $extra = []): int;
```

```
public function delete(): int;
```

```
public function deleteByStringId(string $id): int;
```

```
public function deleteByIntId(int $id): int;
```

```
public function deleteByArray(array $ids): int;
```

```
public function paginate(int $perPage = 15) : LengthAwarePaginator;
```

```
public function create(array $data) : Model;
```

```
public function update(array $data): int;
```

```
public function updateByIntId(array $data, int $id) : Model;
```

```
public function updateByStringId(array $data, string $id) : Model;
```

```
public function byIntId(int $id);
```

```
public function byStringId(string $id);
```

```
public function byIntIdOrFail(int $id) : Model;
```

```
public function byStringIdOrFail(string $id) : Model;
```

```
public function oneByString(string $field, string $value): Model;
```

```
public function oneByInt(string $field, int $value): Model;
```

```
public function oneByStringOrFail(string $field, string $value) : Model;
```

```
public function oneByIntOrFail(string $field, int $value) : Model;
```

```
public function first();
```

```
public function firstOrFail() : Model;
```

### QueryRelate Methods

[](#queryrelate-methods)

```
public function select(array $column = ['*']): QueryRelate;
```

```
public function selectRaw(string $expression, array $bindings = []): QueryRelate;
```

```
public function skip(int $limit): QueryRelate;
```

```
public function take(int $limit): QueryRelate;
```

```
public function groupBy(string $column): QueryRelate;
```

```
public function groupByArray(array $columns): QueryRelate;
```

```
public function orderBy(string $column, string $sort = 'desc'): QueryRelate;
```

```
public function orderByArray(array $columns): QueryRelate;
```

```
public function distinct(): QueryRelate;
```

```
public function where(string $column, string $operator = '=', string $value = ''): QueryRelate;
```

```
public function whereClosure(\Closure $callback): QueryRelate;
```

```
public function orWhereClosure(\Closure $callback): QueryRelate;
```

```
public function orWhere(string $column, string $operator = '=', string $value = ''): QueryRelate;
```

```
public function whereBetween(string $column, array $between): QueryRelate;
```

```
public function orWhereBetween(string $column, array $between): QueryRelate;
```

```
public function whereRaw(string $sql, array $bindings = []): QueryRelate;
```

```
public function orWhereRaw(string $sql, array $bindings = []): QueryRelate;
```

```
public function orWhereNotBetween($column, array $between): QueryRelate;
```

```
public function whereExists(\Closure $callback): QueryRelate;
```

```
public function orWhereExists(\Closure $callback): QueryRelate;
```

```
public function whereNotExists(\Closure $callback): QueryRelate;
```

```
public function orWhereNotExists(\Closure $callback): QueryRelate;
```

```
public function whereIn(string $column, array $values): QueryRelate;
```

```
public function orWhereIn(string $column, array $values): QueryRelate;
```

```
public function whereNotIn(string $column, array $values): QueryRelate;
```

```
public function orWhereNotIn(string $column, array $values): QueryRelate;
```

```
public function whereNull(string $column): QueryRelate;
```

```
public function orWhereNull(string $column): QueryRelate;
```

```
public function whereNotNull(string $column): QueryRelate;
```

```
public function orWhereNotNull(string $column): QueryRelate;
```

```
public function raw(string $sql): QueryRelate;
```

```
public function from(string $table): QueryRelate;
```

```
public function join(string $table, string $one, string $operator = '=', string $two = ''): QueryRelate;
```

```
public function joinClosure(string $table, \Closure $callback): QueryRelate;
```

```
public function leftJoin(string $table, string $first, string $operator = '=', string $two = ''): QueryRelate;
```

```
public function leftJoinClosure(string $table, \Closure $callback): QueryRelate;
```

```
public function rightJoin(string $table, string $first, string $operator = '=', string $two = ''): QueryRelate;
```

```
public function rightJoinClosure(string $table, \Closure $callback): QueryRelate;
```

```
public function callable(callable $callable): QueryRelate;
```

```
public function wheres(array $wheres): QueryRelate;
```

```
public function union(QueryRelate $queryRelate): QueryRelate;
```

```
public function magic(QueryMagic $queryMagic): QueryRelate;
```

```
public function whenMagic(?QueryMagic $queryMagic = null): QueryRelate;
```

```
public function with(string $relation): QueryRelate;
```

```
public function withArray(array $relations): QueryRelate;
```

```
public function without(string $relation): QueryRelate;
```

```
public function withoutArray(array $relations): QueryRelate;
```

```
public function having(string $column, $operator = null, $value = null): QueryRelate;
```

```
public function orHaving(string $column, $operator = null, $value = null): QueryRelate;
```

```
public function havingRaw(string $sql, array $bindings = []): QueryRelate;
```

```
public function orHavingRaw(string $sql, array $bindings = []): QueryRelate;
```

```
public function lockForUpdate(): QueryRelate;
```

```
public function sharedLock(): QueryRelate;
```

License
-------

[](#license)

[MIT license](https://opensource.org/licenses/MIT)

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 55.9% 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 ~46 days

Recently: every ~77 days

Total

26

Last Release

2286d ago

Major Versions

0.1.0 → 2.0.02017-10-09

2.1.17 → v3.x-dev2020-03-26

PHP version history (3 changes)0.0.1PHP &gt;=7.0

2.1.3PHP &gt;=7.1

2.1.7PHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6357263?v=4)[simon](/maintainers/hiword)[@hiword](https://github.com/hiword)

---

Top Contributors

[![hiword](https://avatars.githubusercontent.com/u/6357263?v=4)](https://github.com/hiword "hiword (19 commits)")[![simondole](https://avatars.githubusercontent.com/u/13651984?v=4)](https://github.com/simondole "simondole (12 commits)")[![simon28082](https://avatars.githubusercontent.com/u/22486914?v=4)](https://github.com/simon28082 "simon28082 (3 commits)")

---

Tags

laravelmodelormrepositoryphplaravelormmodelrepository

### Embed Badge

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

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

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k51.0M7.7k](/packages/larastan-larastan)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M88](/packages/mongodb-laravel-mongodb)[prettus/l5-repository

Laravel 8|9|10|11|12|13 - Repositories to the database layer

4.2k11.2M154](/packages/prettus-l5-repository)[laravel/ai

The official AI SDK for Laravel.

1.0k2.1M163](/packages/laravel-ai)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.4M2](/packages/glushkovds-phpclickhouse-laravel)[itpathsolutions/dbstan

Database Standardization and Analysis Tool for Laravel

442.1k](/packages/itpathsolutions-dbstan)

PHPackages © 2026

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