PHPackages                             dezsidog/laravel-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. dezsidog/laravel-repository

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dezsidog/laravel-repository
===========================

laravel repository

4.0.7(6y ago)7282[1 issues](https://github.com/zedisdog/laravel-repository/issues)WTFPLPHPPHP &gt;=7.2.0

Since Nov 18Pushed 6y ago1 watchersCompare

[ Source](https://github.com/zedisdog/laravel-repository)[ Packagist](https://packagist.org/packages/dezsidog/laravel-repository)[ RSS](/packages/dezsidog-laravel-repository/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (3)Versions (21)Used By (0)

laravel repository
==================

[](#laravel-repository)

[![License](https://camo.githubusercontent.com/9969e8b860770cc81fb95fc2bfed51a04448e04f2d445fe0fb72ef940ef61c65/687474703a2f2f7777772e777466706c2e6e65742f77702d636f6e74656e742f75706c6f6164732f323031322f31322f777466706c2d62616467652d312e706e67)](LICENSE)

laravel repositories is used to abstract the data layer, making our application more flexible to maintain.

> bad english

> will be tested soon.

language
--------

[](#language)

[中文](https://github.com/zedisdog/laravel-repository/blob/develop/readme_cn.md)

feature
-------

[](#feature)

- yii expands: you can append `expands=xxx,xx` to url for extra fields by call the relation methods
- filters: you can append `filters[fieldName]=xxx&filters[fieldName2]=xx` to url for search records by given condition.
- custom filters: you can add or cover the filter methods.
- sort: you can append `sorts[name]=asc` to url for sort records by given field.
- custom filters: you can add or cover the sort methods.

install
-------

[](#install)

```
composer require dezsidog/laravel-repository
```

usage
-----

[](#usage)

### create model

[](#create-model)

```
namespace App;

class Post extends Eloquent {

    protected $fillable = [
        'title',
        'author',
        ...
     ];

     ...
}
```

### create repository

[](#create-repository)

```
namespace App\Repository;

use Dezsidog\Repository;
use App\Post;

class PostRepository extends Repository {
    protected $model = Post::class;
}
```

### add repository to controller

[](#add-repository-to-controller)

```
namespace App\Http\Controllers;

use App\Repository\PostRepository;

class PostsController extends BaseController {

    /**
     * @var PostRepository
     */
    protected $posts;

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

### then you can

[](#then-you-can)

```
public function someMethod(){
    $this->posts->find(...);
    $this->posts->findBy(...);
    $this->posts->all();
    $this->posts->paginate(...); // deprecated
    $this->posts->create(...);
    $this->posts->update(...);
    $this->posts->delete(...);
}
```

expands
-------

[](#expands)

if model has relations.

```
namespace App;

use App\User;
use App\Type;

class Post extends Eloquent {

    protected $fillable = [
        'title',
        'author',
        'user_id',
        'type_id',
        ...
     ];

    public function user(){
        return $this->belongTo(User::class);
    }

    public function type(){
        return $this->belongTo(Type::class);
    }
     ...
}
```

### you can

[](#you-can)

- just append expands field to url:

```
https://www.xxxx.com/path/to/post-list?expands=user,type

```

- or make expands auto

```
namespace App\Repository;

use Dezsidog\Repository;
use App\Post;

class PostRepository extends Repository {
    protected $model = Post::class;
    protected $expands = [
        'user',
        'type',
    ];
}
```

filters
-------

[](#filters)

to search something by

```
https://www.xxxx.com/path/to/post-list?filters[type_id]=3&filters[user_id]=1

```

you can use relations

```
https://www.xxxx.com/path/to/post-list?filters[type.id]=3&filters[user.id]=1

```

### custom filters

[](#custom-filters)

add method named `filterBy + key name`, to custom filters.

```
namespace App\Repository;

use Dezsidog\Repository;
use App\Post;
use Illuminate\Database\Eloquent\Builder;

class PostRepository extends Repository {
    protected $model = Post::class;

    public function filterByNameOrTypeName(Builder $query, $value){
        $query->leftJoin('users', 'users.id', '=', 'posts.user_id')
            ->leftJoin('types', 'type.id', '=', 'posts.type_id')
            ->where(function(Builder $query) use ($value) {
                $query->where('users.username', 'like', '%'.$value.'%')
                    ->orWhere('types.name', 'like', '%'.$value.'%')
            });
    }
}
```

then make url:

```
https://www.xxxx.com/path/to/post-list?filters[name-or-type-name]=x

```

### you can just cover the exists field

[](#you-can-just-cover-the-exists-field)

```
namespace App\Repository;

use Dezsidog\Repository;
use App\Post;
use Illuminate\Database\Eloquent\Builder;

class PostRepository extends Repository {
    protected $model = Post::class;

    public function filterByTitle(Builder $query, $value){
        $query->where('posts.title', '=', $value);
    }
}
```

sort
----

[](#sort)

to sort something by

```
https://www.xxxx.com/path/to/post-list?sorts[type_id]=asc&sorts[user_id]=desc

```

you can not use relations

### custom sorts

[](#custom-sorts)

add method named `sortBy + key name`, to custom sorts.

```
namespace App\Repository;

use Dezsidog\Repository;
use App\Post;
use Illuminate\Database\Eloquent\Builder;

class PostRepository extends Repository {
    protected $model = Post::class;

    public function sortByName(Builder $query, $value){
        $query->orderby('name',$value);
    }
}
```

then make url:

```
https://www.xxxx.com/path/to/post-list?filters[name]=asc|desc

```

you can also cover the exists field

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~32 days

Total

19

Last Release

2522d ago

Major Versions

0.9.2 → 1.0.02018-02-02

1.1.2 → 2.0.02018-03-01

2.0.0 → 3.0.02018-03-01

3.0.2 → 4.0.02018-05-25

PHP version history (2 changes)0.9.0PHP &gt;=7.1.0

4.0.0PHP &gt;=7.2.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8280666?v=4)[zed](/maintainers/zedisdog)[@zedisdog](https://github.com/zedisdog)

---

Top Contributors

[![zedisdog](https://avatars.githubusercontent.com/u/8280666?v=4)](https://github.com/zedisdog "zedisdog (88 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[blair2004/nexopos

The Free Modern Point Of Sale System build with Laravel, TailwindCSS and Vue.js.

1.2k2.3k](/packages/blair2004-nexopos)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)[ronasit/laravel-helpers

Provided helpers function and some helper class.

1475.7k13](/packages/ronasit-laravel-helpers)[boomcms/boom-core

Core classes for BoomCMS

193.0k6](/packages/boomcms-boom-core)

PHPackages © 2026

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