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

ActiveLibrary[API Development](/categories/api)

spatie/laravel-query-builder
============================

Easily build Eloquent queries from API requests

7.3.0(2mo ago)4.5k30.7M↓27.7%41120MITPHPPHP ^8.3CI passing

Since Jan 16Pushed 1w ago46 watchersCompare

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

READMEChangelog (10)Dependencies (30)Versions (146)Used By (20)

 [   ![Logo for laravel-query-builder](https://camo.githubusercontent.com/277c99ab7d52519525da933723008fb37ddab79096447b1f8db20a57c3003c4a/68747470733a2f2f7370617469652e62652f7061636b616765732f6865616465722f6c61726176656c2d71756572792d6275696c6465722f68746d6c2f6c696768742e77656270)  ](https://spatie.be/open-source?utm_source=github&utm_medium=banner&utm_campaign=laravel-query-builder)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://github.com/spatie/laravel-query-builder/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-query-builder/actions/workflows/run-tests.yml/badge.svg)[![Code Style Status](https://github.com/spatie/laravel-query-builder/actions/workflows/pint.yml/badge.svg)](https://github.com/spatie/laravel-query-builder/actions/workflows/pint.yml/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/b06894bff55703ad61b99a25a3d21a081bce61e0bfee82d517a8c73073031d3e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d71756572792d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-query-builder)

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/v7/features/filtering/)

### Grouping multiple filters with OR/AND: `/users?filter[q]=John`:

[](#grouping-multiple-filters-with-orand-usersfilterqjohn)

```
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

$users = QueryBuilder::for(User::class)
    ->allowedFilters(
        AllowedFilter::partial('name'),
        AllowedFilter::partial('full_name'),
        AllowedFilter::groupOr('q', [
            AllowedFilter::partial('name'),
            AllowedFilter::partial('full_name'),
        ]),
    )
    ->get();

// /users?filter[q]=John
//   → WHERE (name LIKE '%John%' OR full_name LIKE '%John%')
//
// /users?filter[q]=John&filter[name]=Doe
//   → WHERE name LIKE '%Doe%' AND (name LIKE '%John%' OR full_name LIKE '%John%')
```

`AllowedFilter::groupAnd()` is also available. Members can be any `AllowedFilter` type and the shorthand value is broadcast to every member.

[Read more about the JSON:API Fancy Filters recommendation this feature follows.](https://gist.github.com/e0ipso/efcc4e96ca2aed58e32948e4f70c2460)

### 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/v7/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/v7/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/v7/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/v7/installation-setup](https://spatie.be/docs/laravel-query-builder/v7/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

84

—

ExcellentBetter than 100% of packages

Maintenance94

Actively maintained with recent releases

Popularity79

Solid adoption and visibility

Community56

Growing community involvement

Maturity96

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~22 days

Recently: every ~11 days

Total

139

Last Release

63d ago

Major Versions

v3.x-dev → 5.1.02022-11-28

v4.x-dev → 5.1.12022-12-02

5.8.1 → 6.0.02024-05-10

v5.x-dev → 6.3.02024-12-23

v6.x-dev → 7.0.02026-03-15

PHP version history (8 changes)0.0.1PHP ^7.1

1.15.0PHP ^7.2

3.2.0PHP ^7.3

3.3.4PHP ^7.3|^8.0

v1.x-devPHP ^7.1|^8.0

5.0.0PHP ^8.0

5.8.0PHP ^8.2

7.0.0PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (408 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (316 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (24 commits)")[![dominikb](https://avatars.githubusercontent.com/u/28777818?v=4)](https://github.com/dominikb "dominikb (15 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (13 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (12 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (11 commits)")[![stevebauman](https://avatars.githubusercontent.com/u/6421846?v=4)](https://github.com/stevebauman "stevebauman (10 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)")[![lorenzolosa](https://avatars.githubusercontent.com/u/11164571?v=4)](https://github.com/lorenzolosa "lorenzolosa (6 commits)")[![alexkart](https://avatars.githubusercontent.com/u/8249105?v=4)](https://github.com/alexkart "alexkart (6 commits)")[![klimov-paul](https://avatars.githubusercontent.com/u/1482054?v=4)](https://github.com/klimov-paul "klimov-paul (5 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (5 commits)")[![enricodelazzari](https://avatars.githubusercontent.com/u/10452445?v=4)](https://github.com/enricodelazzari "enricodelazzari (4 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (4 commits)")[![liamduckett](https://avatars.githubusercontent.com/u/116881406?v=4)](https://github.com/liamduckett "liamduckett (4 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (4 commits)")[![vyuldashev](https://avatars.githubusercontent.com/u/1809081?v=4)](https://github.com/vyuldashev "vyuldashev (4 commits)")[![ntzm](https://avatars.githubusercontent.com/u/3888578?v=4)](https://github.com/ntzm "ntzm (4 commits)")

---

Tags

apihacktoberfestlaravelphpspatielaravel-query-builder

###  Code Quality

TestsPest

Static AnalysisPHPStan

### Embed Badge

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

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5021.9k](/packages/simplestats-io-laravel-client)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.8k3](/packages/defstudio-telegraph)[mike-bronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k91.0k1](/packages/mike-bronner-laravel-model-caching)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45444.2k1](/packages/pressbooks-pressbooks)

PHPackages © 2026

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