PHPackages                             mayconbordin/reloquent - 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. mayconbordin/reloquent

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

mayconbordin/reloquent
======================

Repository data layer for Laravel 5

41131[3 issues](https://github.com/mayconbordin/reloquent/issues)PHP

Since Dec 29Pushed 10y ago1 watchersCompare

[ Source](https://github.com/mayconbordin/reloquent)[ Packagist](https://packagist.org/packages/mayconbordin/reloquent)[ RSS](/packages/mayconbordin-reloquent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![reloquent](https://raw.githubusercontent.com/mayconbordin/reloquent/master/artwork/logo.png)](https://raw.githubusercontent.com/mayconbordin/reloquent/master/artwork/logo.png)
==================================================================================================================================================================================

[](#)

[![Build Status](https://camo.githubusercontent.com/dc3fbe485824598c683fae5721bac8a70809540dcdcc218a7e335360fe481490/68747470733a2f2f7472617669732d63692e6f72672f6d6179636f6e626f7264696e2f72656c6f7175656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mayconbordin/reloquent)

Verbose Repository Data Access Layer for Laravel 5.

Yet Another Repository package?
-------------------------------

[](#yet-another-repository-package)

If you search within Github you [can find](https://github.com/search?l=PHP&q=repository+laravel&ref=searchresults&type=Repositories&utf8=%E2%9C%93)at least a dozen implementations of the repository pattern for Laravel 4 and 5. For Laravel 5, specifically, the best ones are [prettus/l5-repository](https://github.com/prettus/l5-repository) and [Bosnadev/Repositories](https://github.com/Bosnadev/Repositories).

The difference between these packages and this one is a more flexible interface, enabling more complex queries through the Repository. And a lot of tests.

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

[](#installation)

Add

```
"mayconbordin/reloquent": "dev-master"
```

to your composer.json. Then run `composer install` or `composer update`.

Add the Service Provider to the `providers` array in `config/app.php`:

```
'providers' => array(
    ...
    'Mayconbordin\Reloquent\Providers\ReloquentServiceProvider',
)
```

And then publish the configurations

```
php artisan vendor:publish
```

API &amp; Usage
---------------

[](#api--usage)

In the following sections examples will be given for all the interface methods available in the repository.

> Assume we have a `Post` class model with columns: `id`, `title`, `content`, `category_id` and `author_id`.

### `findAllBy`

[](#findallby)

```
// to find all posts that belong to a single category
$repository->findAllByCategoryId(1);

// fetch also the post tags and category
$repository->findAllByCategoryIdWith(1, ['tags', 'category']);

// we can also order the results by id in ascending order
$repository->findAllByCategoryIdOrderById(1);

// or descending
$repository->findAllByCategoryIdOrderByDescId(1);

// we could then limit the number of results returned to 15
$repository->findAllByCategoryIdOrderByIdLimit(1, 15);

// or even paginate the results, with 15 records per page
$repository->findAllByCategoryIdOrderByIdPaginated(1, 15);

// we can also get posts within a list of categories
$repository->findAllByInCategoryId([1, 2, 3, 4]);

// or filter the posts by category and author
$repository->findAllByCategoryIdAndAuthorId(1, 2);

// or by category or author
$repository->findAllByCategoryIdOrAuthorId(1, 2);
```

### `findBy`

[](#findby)

```
// find a single post by author id
$repository->findByAuthorId(1);

// or with author id AND category id
$repository->findByCategoryIdAndAuthorId(1, 2);

// or with author id OR category id
$repository->findByCategoryIdOrAuthorId(1, 2);

// fetch also the post tags and category objects
$repository->findByAuthorIdWith(1, ['tags', 'category']);
```

### `create`, `update` and `delete`

[](#create-update-and-delete)

```
$attributes = [
  'title'       => 'Post Title',
  'content'     => 'Post content...',
  'category_id' => 1,
  'author_id'   => 1
];

// create a new post
$post = $repository->create($attributes);

// update the title of the post
$updatedPost = $repository->update(['title' => 'Update Post Title'], $post->id);

// and delete it
$repository->delete($updatedPost->id);
```

#### Creating and deleting models with their relations

[](#creating-and-deleting-models-with-their-relations)

By default the repository doesn't care about the relations a model might have, which means that you can't give an instance of a related model as an attribute value to the `create` method. You also can't have the related objects removed before the object in question, and if the database is not configured to remove the children objects (`DELETE CASCADE`) the operation will fail.

To make the repository aware of the relations, add the attribute `relations` to the repository implementations, like this:

```
class PostRepository extends BaseRepository
{
    protected $relations = ['category', 'author'];

    ...
}
```

Now you can create a new post like this:

```
$attributes = [
  'title'    => 'Post Title',
  'content'  => 'Post content...',
  'category' => Category::find(1),
  'author'   => Author::find(1)
];

// create a new post
$post = $repository->create($attributes);
```

If a post could also have tags, you could create a post with tags like this:

```
$attributes = [
  'title'    => 'Post Title',
  'content'  => 'Post content...',
  'category' => Category::find(1),
  'author'   => Author::find(1),
  'tags'     => [1, 2, 3, 4]
];
```

`BelongsTo` relations are saved using the `associate` method, while `BelongsToMany` use the `attach` method. And when a post is deleted, the repository will iterate over the defined relations and remove them. For relations that are instances of `HasOneOrMany` the method `delete` is used, and for `BelongsToMany` the `detach` method.

### `exists`, `find` and `findByField`

[](#exists-find-and-findbyfield)

```
// check if a post exists, returns a boolean
$repository->exists(1);

// find a post by id
$repository->find(1);

// bring also its relations
$repository->find(1, ['author', 'tags']);

// find a post by author
$repository->findByField('author_id', 1);

// find a post whose title starts with 'A'
$repository->findByField('title', 'A%', 'LIKE');

// and you can also bring the post's relations
$repository->findByField('title', 'A%', 'LIKE', ['author', 'tags']);
```

### `findWhere`

[](#findwhere)

The `findWhere` method is not as pretty as the other methods. The first argument is an associative array of `where` statements. An statement is an array that contains the value of the field, and optionally the operator and the statement.

```
$repository->findWhere([
    'title' => ['test'],        // WHERE title = 'test'
    'category_id' => ['!=', 2], // AND category_id != 2
    'author_id' => ['or', 1]    // OR author_id = 1
    'content' => ['or', 'like', '%test%']    // OR content LIKE %test%
]);
```

### `all`, `findAllByField` and `findAllWhere`

[](#all-findallbyfield-and-findallwhere)

```
// get all posts
$repository->all();

// get all posts ordered by title
$repository->all('title:asc');

// get all posts ordered by id and title
$repository->all(['id:asc', 'title:asc']);

// get all posts by author
$repository->findAllByField('author_id', 1);

// get all posts not by author
$repository->findAllByField('author_id', 1, '!=');

// get all posts by author ordered by title
$repository->findAllByField('author_id', 1, '=', 'title:asc');

// get 15 posts by author ordered by title
$repository->findAllByField('author_id', 1, '=', 'title:asc', ['author'], 15);

// same as the above query
$repository->findAllWhere(['author_id' => 1], 'title:desc', ['author'], 15);
```

### `paginate`, `findAllByFieldPaginated` and `findAllWherePaginated`

[](#paginate-findallbyfieldpaginated-and-findallwherepaginated)

```
// get all posts
$repository->paginate();

// get 15 posts per page ordered by title
$repository->paginate(15, 'title:asc');

// get 15 posts per page posts ordered by id and title
$repository->paginate(15, ['id:asc', 'title:asc']);

// get 15 posts per page posts ordered by id and title with author
$repository->paginate(15, ['id:asc', 'title:asc'], ['author']);

// get posts by author
$repository->findAllByFieldPaginated('author_id', 1);

// get posts not by author
$repository->findAllByFieldPaginated('author_id', 1, '!=');

// get posts by author ordered by title
$repository->findAllByFieldPaginated('author_id', 1, '=', 'title:asc');

// get 15 posts per page by author ordered by title
$repository->findAllByFieldPaginated('author_id', 1, '=', 'title:asc', 15, ['author']);

// same as the above query
$repository->findAllWherePaginated(['author_id' => 1], 15, 'title:desc', ['author']);
```

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance5

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/42528727c83dd30f55c2a01ced50c7f22aeaf5a0bf83503d862e15448733a81a?d=identicon)[mayconbordin](/maintainers/mayconbordin)

---

Top Contributors

[![mayconbordin](https://avatars.githubusercontent.com/u/597104?v=4)](https://github.com/mayconbordin "mayconbordin (38 commits)")

### Embed Badge

![Health badge](/badges/mayconbordin-reloquent/health.svg)

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M542](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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