PHPackages                             bjerke/api-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. [HTTP &amp; Networking](/categories/http)
4. /
5. bjerke/api-query-builder

ActiveLibrary[HTTP &amp; Networking](/categories/http)

bjerke/api-query-builder
========================

A query builder for Laravel that parses the request and uses Eloquent ORM to query database

v1.8.0(2y ago)267.7k2[1 issues](https://github.com/jesperbjerke/laravel-api-query-builder/issues)1MITPHPPHP ^7.3 || ^8.0

Since Jun 24Pushed 2y ago3 watchersCompare

[ Source](https://github.com/jesperbjerke/laravel-api-query-builder)[ Packagist](https://packagist.org/packages/bjerke/api-query-builder)[ RSS](/packages/bjerke-api-query-builder/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (10)Dependencies (3)Versions (15)Used By (1)

Laravel ApiQueryBuilder
=======================

[](#laravel-apiquerybuilder)

A library to enable clients to query models in a very dynamic way, mimicking the Eloquent ORM.

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

[](#installation)

```
composer require bjerke/api-query-builder
```

### Configuration

[](#configuration)

There is not much to configure, but one thing that can be configured is what collation to use when querying with localized order clauses. It is preconfigured to use `utf8mb4_swedish_ci` for the `sv` and `sv-SE` locales. If you don't need any special collations for your other locales, there's no need to publish this configuration. If you do want to add other collations for specific locales however, you need to publish the configuration file from this library so you can change it in your own application. To do this run the following artisan command:

```
php artisan vendor:publish --provider="Bjerke\ApiQueryBuilder\QueryBuilderServiceProvider"
```

You will now have a `querybuilder.php` config file in `/config` where you can add additional locales =&gt; collation combinations

Usage
-----

[](#usage)

To use the query builder, you will use the 2 main components of this library. The trait `QueryBuilderModelTrait` and the builder itself `QueryBuilder`.

The trait is there to help the builder validate requested fields, relations, appendable attributes and counts. As well as some helper methods. Read more on how to use [relations](#with), [appendable attributes](#appends) and [counts](#counts) in their own descriptions below. This trait needs to be included in the models you want to use the query builder on.

In your controller method, you can then use the query builder to compile an Eloquent builder class based on the request like:

```
public function index(Request $request)
{
    // Setup the builder
    $queryBuilder = new QueryBuilder(new MyModel, $request);

    // Parse the request and return an Eloquent Builder instance
    $query = $queryBuilder->build();

    // The instance can be extended/modified freely just as any other Eloquent Builder instance
    // For example, maybe we want to enable the option to turn pagination on/off?
    if (($pagination = $request->input('paginate')) !== null &&
        ($pagination === false || $pagination === 'false' || $pagination === '0')
    ) {
        return $query->get();
    }

    $perPage = $request->input('per_page');

    return $query->paginate($perPage)->appends($request->except('page'));
}
```

### Available query methods

[](#available-query-methods)

Most methods include an `or` counterpart, that will allow you to create OR statements in your queries. Just like Eloquent. For example `where` and `orWhere`.

- [where](#where--orwhere)
- [whereIn](#wherein--orwherein)
- [whereNotIn](#wherenotin--orwherenotin)
- [whereBetween](#wherebetween--orwherebetween)
- [whereNotBetween](#wherenotbetween--orwherenotbetween)
- [whereNull](#wherenull--orwherenull)
- [whereNotNull](#wherenotnull--orwherenotnull)
- [whereHas](#wherehas--orwherehas)
- [whereDoesntHave](#wheredoesnthave--orwheredoesnthave)
- [whereDate](#wheredate) (whereDate / whereMonth / whereDay / whereYear / whereTime),
- [search](#search)
- [select](#select)
- [orderBy](#orderby)
- [groupBy](#groupby)
- [limit](#limit)
- [with](#with)
- [appends](#appends)
- [counts](#counts)
- [pagination](#pagination--per_page)

---

### where / orWhere

[](#where--orwhere)

Executes a where statement. It can be defined in a couple of ways. The following will do an exact match where `first_name` equals `test`

```
?where[first_name]=test

```

You can also do more advanced matching by defining an operator (`=`, `!=`, `like`, `>`, ``, `
