PHPackages                             williamoliveira/eloquent-array-query-builder - 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. williamoliveira/eloquent-array-query-builder

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

williamoliveira/eloquent-array-query-builder
============================================

Query Laravel Eloquent with and array

1.4.5(5y ago)189.5k↓50%11[1 PRs](https://github.com/williamoliveira/eloquent-array-query-builder/pulls)1MITPHPCI failing

Since Apr 17Pushed 5y ago6 watchersCompare

[ Source](https://github.com/williamoliveira/eloquent-array-query-builder)[ Packagist](https://packagist.org/packages/williamoliveira/eloquent-array-query-builder)[ RSS](/packages/williamoliveira-eloquent-array-query-builder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (21)Used By (1)

[![Travis CI Build Status](https://camo.githubusercontent.com/07f7be8111923da9713e8c38ed66150c82fcd59fe47bd9a38b836b8edcfaf3bd/68747470733a2f2f7472617669732d63692e6f72672f77696c6c69616d6f6c6976656972612f656c6f7175656e742d61727261792d71756572792d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/williamoliveira/eloquent-array-query-builder)[![Latest Stable Version](https://camo.githubusercontent.com/a86d7764398452425bbb631cc4081f79cc6cf98a2f7c9ca49a7e215d1e96c97a/68747470733a2f2f706f7365722e707567782e6f72672f77696c6c69616d6f6c6976656972612f656c6f7175656e742d61727261792d71756572792d6275696c6465722f762f737461626c65)](https://packagist.org/packages/williamoliveira/eloquent-array-query-builder)[![Total Downloads](https://camo.githubusercontent.com/fa18742af157e95d0e7c9a7ce3d4f564fb1fb9521485bffb94696606a5333521/68747470733a2f2f706f7365722e707567782e6f72672f77696c6c69616d6f6c6976656972612f656c6f7175656e742d61727261792d71756572792d6275696c6465722f646f776e6c6f616473)](https://packagist.org/packages/williamoliveira/eloquent-array-query-builder)[![License](https://camo.githubusercontent.com/2e4347737bf154593b594aa0287ad0e90fb44a938857e2008371ddc2b2437506/68747470733a2f2f706f7365722e707567782e6f72672f77696c6c69616d6f6c6976656972612f656c6f7175656e742d61727261792d71756572792d6275696c6465722f6c6963656e7365)](https://packagist.org/packages/williamoliveira/eloquent-array-query-builder)

Why this nonsense?
------------------

[](#why-this-nonsense)

So you can have easy to use query "language" to query your data, without the need to write long conditional queries by hand, very useful for REST APIs.

How to install
--------------

[](#how-to-install)

`composer require williamoliveira/eloquent-array-query-builder`

How to use
----------

[](#how-to-use)

We let the wiring of the request to the model to you, so you can use it wherever you want.

Example in a controller:

```
public function index(Request $request, \Williamoliveira\ArrayQueryBuilder\ArrayBuilder $arrayBuilder)
{
    $query = User::query();
    $query = $arrayBuilder->apply($query, $request->all());

    return $query->paginate($request->get('per_page')); // Note it does not do pagination or call get(),
                                                        // you need to do it yourself
}
```

You can also use the ArrayQueryable trait in your model:

```
 // Model
 class User extends Model{
     use \Williamoliveira\ArrayQueryBuilder\Traits\ArrayQueryable;
 // ...

 // Usage
 return User::arrayQuery($request->all())->get(); //static
 return (new User())->newArrayQuery($request->all())->get(); //instance
```

#### Query format

[](#query-format)

Here is a example of what a query can look like:

```
$exampleArrayQuery = [
    'where' => [
        'name' => ['like' => '%joao%'],
        'created_at' => [
            'between'  => [
                 '2014-10-10',
                 '2015-10-10',
            ],
        ],
        'or' => [                             // nested boolean where clauses
            'foo' => 'bar',
            'baz' => 'qux',
        ],
    ],
    'fields' => ['id', 'name', 'created_at'],
    'order' => 'name',
    'include' => [                            // relations, can have where, order and fields
        'permissions' => true,
        'roles' => [
            'where' => [
                'name' => 'admin',
            ],
            'fields' => ['id', 'name'],
            'order' => 'name DESC',
        ],
    ],
    'groupBy' => ['foo', 'bar', 'baz'],
    'having' => [
        'foo' => 'x',
        'bar' => ['in' => ['1', '2']],
        'baz' => ['neq' => '3'],
    ],
    'offset' => 5,
    'limit' => 15,
];
```

Just as a reference to people building REST APIs, the same query as a query string:

```
?where[name][like]=%joao%
&where[created_at][between][]=2014-10-10
&where[created_at][between][]=2015-10-10
&where[or][foo]=bar
&where[or][baz]=qux
&fields[]=id
&fields[]=name
&fields[]=created_at
&order=name
&include[permissions]=true
&include[roles][where][name]=admin
&include[roles][fields][]=id
&include[roles][fields][]=name
&include[roles][order]=name DESC
&groupBy[]=foo
&groupBy[]=bar
&groupBy[]=baz
&having[foo]=x
&having[bar][in][]=1
&having[bar][in][]=2
&having[baz][neq]=3
&offset=5
&limit=15

```

Tip: for Javascript you can create a query string using [Qs](https://github.com/ljharb/qs).

#### Where/Having operators aliases

[](#wherehaving-operators-aliases)

```
'eq' => '=',
'neq' => '',
'gt' => '>',
'gte' => '>=',
'lt' => '
