Skip to content

essivision/eloquent-array-query-builder

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Why this nonsence?

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

⚠️ It still in alpha, isn't well tested and the API may change a bit so it's recommended to be used in production yet

How to install

composer require williamoliveira/eloquent-array-query-builder:dev-master

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,
                                                        // you need to do it youserlf
}

You can also use the ArrayQueryable trait in your model:

 // Model
 class User extends Model{
     use \Williamoliveira\ArrayQueryBuilder\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:

$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'
        ]
    ]
];

The same query as a query string:

/your-route?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

About

Query Laravel Eloquent with and array

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%