2017 © Pedro Peláez
 

library eloquent-array-query-builder

Query Laravel Eloquent with and array

image

williamoliveira/eloquent-array-query-builder

Query Laravel Eloquent with and array

  • Thursday, May 3, 2018
  • by williamoliveira
  • Repository
  • 5 Watchers
  • 10 Stars
  • 3,626 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 17 Versions
  • 24 % Grown

The README.md

Travis CI Build Status Latest Stable Version Total Downloads License, (*1)

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., (*2)

How to install

composer require williamoliveira/eloquent-array-query-builder, (*3)

How to use

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

Example in a controller:, (*5)

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:, (*6)

 // 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

Here is a example of what a query can look like:, (*7)

$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:, (*8)

?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., (*9)

Where/Having operators aliases

'eq' => '=',
'neq' => '<>',
'gt' => '>',
'gte' => '>=',
'lt' => '<',
'lte' => '<=',
'nlike' => 'not like',
'nin' => 'not in',
'notnull' => 'not null',
'nn' => 'not null',
'inq' => 'in'

The Versions

03/05 2018

dev-master

9999999-dev

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

03/05 2018

v1.4.0

1.4.0.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

03/05 2018

dev-develop

dev-develop

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

22/02 2018

v1.3.1

1.3.1.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

03/10 2017

v1.3.0

1.3.0.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

03/10 2017

v1.2.0

1.2.0.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

24/09 2017

v1.1.4

1.1.4.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

24/09 2017

dev-williamoliveira-patch-1

dev-williamoliveira-patch-1

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

15/04 2017

v1.1.3

1.1.3.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

19/12 2016

v1.1.2

1.1.2.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

19/12 2016

v1.1.1

1.1.1.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

18/12 2016

v1.1.0

1.1.0.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

The Development Requires

by William Oliveira

29/08 2016

v1.0.0

1.0.0.0

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

by William Oliveira

05/07 2016

dev-dev

dev-dev

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

by William Oliveira

15/05 2016

v1.0-alpha.2

1.0.0.0-alpha2

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

by William Oliveira

18/04 2016

v1.0-alpha.1

1.0.0.0-alpha1

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

by William Oliveira

17/04 2016

v1.0-alpha

1.0.0.0-alpha

Query Laravel Eloquent with and array

  Sources   Download

MIT

The Requires

 

by William Oliveira