PHPackages                             at-lab/laravel-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. at-lab/laravel-query-builder

ActiveLibrary

at-lab/laravel-query-builder
============================

Easily build Eloquent queries from API requests

v1.0.0(1y ago)0676↓100%MITPHPPHP ^8.2

Since Apr 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/paataD/laravel-query-builder)[ Packagist](https://packagist.org/packages/at-lab/laravel-query-builder)[ Docs](https://github.com/spatie/laravel-query-builder)[ RSS](/packages/at-lab-laravel-query-builder/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (9)Versions (2)Used By (0)

Build Eloquent queries from API requests
========================================

[](#build-eloquent-queries-from-api-requests)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4e0d6643bad272dbce941b7a77c1bb874065ea3537828d2126d1f076dd654488/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d71756572792d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-query-builder)[![Test Status](https://camo.githubusercontent.com/a0885c500ddb31039850c032248115ca3cf27aa27ee9dcdbd6194006d2a136be/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f72756e2d74657374732e796d6c3f6c6162656c3d7465737473266272616e63683d6d61696e)](https://camo.githubusercontent.com/a0885c500ddb31039850c032248115ca3cf27aa27ee9dcdbd6194006d2a136be/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f72756e2d74657374732e796d6c3f6c6162656c3d7465737473266272616e63683d6d61696e)[![Code Style Status](https://camo.githubusercontent.com/9326f0b5e1da41bc6c7775fec2efd7fb43438dc66b238cf125da16260fee8cb0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65266272616e63683d6d61696e)](https://camo.githubusercontent.com/9326f0b5e1da41bc6c7775fec2efd7fb43438dc66b238cf125da16260fee8cb0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65266272616e63683d6d61696e)[![Total Downloads](https://camo.githubusercontent.com/b06894bff55703ad61b99a25a3d21a081bce61e0bfee82d517a8c73073031d3e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d71756572792d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-query-builder)

This package allows you to filter, sort and include eloquent relations based on a request. The `QueryBuilder` used in this package extends Laravel's default Eloquent builder. This means all your favorite methods and macros are still available. Query parameter names follow the [JSON API specification](http://jsonapi.org/) as closely as possible.

Basic usage
-----------

[](#basic-usage)

### Filter a query based on a request: `/users?filter[name]=John`:

[](#filter-a-query-based-on-a-request-usersfilternamejohn)

```
use Spatie\QueryBuilder\QueryBuilder;

$users = QueryBuilder::for(User::class)
    ->allowedFilters('name')
    ->get();

// all `User`s that contain the string "John" in their name
```

[Read more about filtering features like: partial filters, exact filters, scope filters, custom filters, ignored values, default filter values, ...](https://spatie.be/docs/laravel-query-builder/v5/features/filtering/)

### Including relations based on a request: `/users?include=posts`:

[](#including-relations-based-on-a-request-usersincludeposts)

```
$users = QueryBuilder::for(User::class)
    ->allowedIncludes('posts')
    ->get();

// all `User`s with their `posts` loaded
```

[Read more about include features like: including nested relationships, including relationship count, custom includes, ...](https://spatie.be/docs/laravel-query-builder/v5/features/including-relationships/)

### Sorting a query based on a request: `/users?sort=id`:

[](#sorting-a-query-based-on-a-request-userssortid)

```
$users = QueryBuilder::for(User::class)
    ->allowedSorts('id')
    ->get();

// all `User`s sorted by ascending id
```

[Read more about sorting features like: custom sorts, sort direction, ...](https://spatie.be/docs/laravel-query-builder/v5/features/sorting/)

### Works together nicely with existing queries:

[](#works-together-nicely-with-existing-queries)

```
$query = User::where('active', true);

$userQuery = QueryBuilder::for($query) // start from an existing Builder instance
    ->withTrashed() // use your existing scopes
    ->allowedIncludes('posts', 'permissions')
    ->where('score', '>', 42); // chain on any of Laravel's query builder methods
```

### Selecting fields for a query: `/users?fields[users]=id,email`

[](#selecting-fields-for-a-query-usersfieldsusersidemail)

```
$users = QueryBuilder::for(User::class)
    ->allowedFields(['id', 'email'])
    ->get();

// the fetched `User`s will only have their id & email set
```

[Read more about selecting fields.](https://spatie.be/docs/laravel-query-builder/v5/features/selecting-fields/)

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/34a9aa47ed3155462cc3b66ff5b4252f457b4e911ccaf26eb9c6c62ec4b031f8/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d71756572792d6275696c6465722e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-query-builder)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

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

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-query-builder
```

Read the installation notes on the docs site: [https://spatie.be/docs/laravel-query-builder/v5/installation-setup](https://spatie.be/docs/laravel-query-builder/v5/installation-setup/).

Documentation
-------------

[](#documentation)

You can find the documentation on .

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the media library? Feel free to [create an issue on GitHub](https://github.com/spatie/laravel-query-builder/issues), we'll try to address it as soon as possible.

If you've found a bug regarding security please mail  instead of using the issue tracker.

### Upgrading

[](#upgrading)

Please see [UPGRADING.md](UPGRADING.md) for details.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

### Security

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Alex Vanderbist](https://github.com/AlexVanderbist)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 63% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

705d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9911260284d826ff962b9f2d7f7acfbffc00f6b207531d6887e2ea9f9eb28cb2?d=identicon)[paata](/maintainers/paata)

---

Top Contributors

[![paataD](https://avatars.githubusercontent.com/u/9696430?v=4)](https://github.com/paataD "paataD (2 commits)")

---

Tags

spatielaravel-query-builder

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/at-lab-laravel-query-builder/health.svg)

```
[![Health](https://phpackages.com/badges/at-lab-laravel-query-builder/health.svg)](https://phpackages.com/packages/at-lab-laravel-query-builder)
```

###  Alternatives

[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k25.7M187](/packages/spatie-laravel-query-builder)[spatie/laravel-activitylog

A very simple activity logger to monitor the users of your website or application

5.8k43.4M264](/packages/spatie-laravel-activitylog)[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k36.2M412](/packages/spatie-laravel-medialibrary)[spatie/laravel-translatable

A trait to make an Eloquent model hold translations

2.4k22.1M350](/packages/spatie-laravel-translatable)[spatie/laravel-webhook-client

Receive webhooks in Laravel apps

1.2k11.2M51](/packages/spatie-laravel-webhook-client)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k7.9M42](/packages/spatie-laravel-responsecache)

PHPackages © 2026

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