PHPackages                             jasonstealer/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. [API Development](/categories/api)
4. /
5. jasonstealer/laravel-query-builder

ActiveLibrary[API Development](/categories/api)

jasonstealer/laravel-query-builder
==================================

Easily build Eloquent queries from API requests

5.0.33(3y ago)024MITPHPPHP ^8.0

Since Jan 16Pushed 3y agoCompare

[ Source](https://github.com/jasonstealer/laravel-query-builder)[ Packagist](https://packagist.org/packages/jasonstealer/laravel-query-builder)[ Docs](https://github.com/spatie/laravel-query-builder)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/jasonstealer-laravel-query-builder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (8)Versions (100)Used By (0)

[![](https://camo.githubusercontent.com/2bedf63f24cda7efab02da955dc11fb7ef8a060e2f26b73c33a7aac84529b8a3/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31)](https://supportukrainenow.org)

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/9fa4cff4b399d590463c4d8bc04d51c5ef6356329b94c3aa25deff06f00246bf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f72756e2d74657374733f6c6162656c3d7465737473)](https://camo.githubusercontent.com/9fa4cff4b399d590463c4d8bc04d51c5ef6356329b94c3aa25deff06f00246bf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f72756e2d74657374733f6c6162656c3d7465737473)[![Code Style Status](https://camo.githubusercontent.com/cec25b5e0932816d48b7c96551484822482c6fdf10885499018d5cc273e0b471/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://camo.githubusercontent.com/cec25b5e0932816d48b7c96551484822482c6fdf10885499018d5cc273e0b471/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d71756572792d6275696c6465722f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)[![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

35

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 50.7% 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

Every ~17 days

Recently: every ~4 days

Total

99

Last Release

1296d ago

Major Versions

v2.x-dev → 3.4.02021-05-20

v1.x-dev → 3.4.22021-07-05

3.6.0 → 4.0.02021-10-20

3.6.2 → 5.0.312022-07-29

4.0.5 → 5.0.332022-10-24

PHP version history (7 changes)0.0.1PHP ^7.1

1.15.0PHP ^7.2

2.8.3PHP ^7.3

3.3.4PHP ^7.3|^8.0

1.18.0PHP ^7.1|^8.0

5.0.31PHP ^8.0

4.0.2PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/f5b5d8fea144929230bfe681f65f69bc677b69d3a306ec7c5a01f117dc5957ef?d=identicon)[jasonstealer](/maintainers/jasonstealer)

---

Top Contributors

[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (344 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (233 commits)")[![dominikb](https://avatars.githubusercontent.com/u/28777818?v=4)](https://github.com/dominikb "dominikb (15 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (8 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![cornelp](https://avatars.githubusercontent.com/u/10222052?v=4)](https://github.com/cornelp "cornelp (6 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (5 commits)")[![klimov-paul](https://avatars.githubusercontent.com/u/1482054?v=4)](https://github.com/klimov-paul "klimov-paul (5 commits)")[![vyuldashev](https://avatars.githubusercontent.com/u/1809081?v=4)](https://github.com/vyuldashev "vyuldashev (4 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (4 commits)")[![ntzm](https://avatars.githubusercontent.com/u/3888578?v=4)](https://github.com/ntzm "ntzm (4 commits)")[![oanhnn](https://avatars.githubusercontent.com/u/1757120?v=4)](https://github.com/oanhnn "oanhnn (3 commits)")[![Medalink](https://avatars.githubusercontent.com/u/201221?v=4)](https://github.com/Medalink "Medalink (3 commits)")[![robsontenorio](https://avatars.githubusercontent.com/u/118955?v=4)](https://github.com/robsontenorio "robsontenorio (3 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (3 commits)")[![marcoocram](https://avatars.githubusercontent.com/u/6926933?v=4)](https://github.com/marcoocram "marcoocram (3 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (2 commits)")[![amaelftah](https://avatars.githubusercontent.com/u/17250137?v=4)](https://github.com/amaelftah "amaelftah (2 commits)")[![rainerkent](https://avatars.githubusercontent.com/u/17761544?v=4)](https://github.com/rainerkent "rainerkent (2 commits)")[![rudashi](https://avatars.githubusercontent.com/u/5038647?v=4)](https://github.com/rudashi "rudashi (2 commits)")

---

Tags

spatielaravel-query-builder

###  Code Quality

TestsPest

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k26.9M220](/packages/spatie-laravel-query-builder)[spatie/laravel-activitylog

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

5.8k45.4M309](/packages/spatie-laravel-activitylog)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.0M60](/packages/spatie-laravel-honeypot)[spatie/laravel-health

Monitor the health of a Laravel application

85810.0M83](/packages/spatie-laravel-health)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)

PHPackages © 2026

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