PHPackages                             wpzag/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. [Database &amp; ORM](/categories/database)
4. /
5. wpzag/laravel-query-builder

ActiveLibrary[Database &amp; ORM](/categories/database)

wpzag/laravel-query-builder
===========================

Build up Eloquent queries from the Query string parameters of the request .

v1.0.2(3y ago)3111MITPHPPHP ^8.2

Since Jul 5Pushed 3y ago1 watchersCompare

[ Source](https://github.com/wpzag/laravel-query-builder)[ Packagist](https://packagist.org/packages/wpzag/laravel-query-builder)[ Docs](https://github.com/wpzag/laravel-query-builder)[ RSS](/packages/wpzag-laravel-query-builder/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (3)Dependencies (12)Versions (4)Used By (0)

Build up Eloquent queries from the Query string parameters of the reuqest .
===========================================================================

[](#build-up-eloquent-queries-from-the-query-string-parameters-of-the-reuqest-)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ed358bba84f76c7279a71f1b45980da9ab89a6e353feb0a08c398b6927d7cb98/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f77707a61672f6c61726176656c2d71756572792d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wpzag/laravel-query-builder)[![GitHub Tests Action Status](https://camo.githubusercontent.com/faa7e1bdf92b0a229d06d3d8f39091abf1828cd108b43a1fc489657744f0eeef/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f77707a61672f6c61726176656c2d71756572792d6275696c6465722f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/wpzag/laravel-query-builder/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/dece565278ffb80876f1fe7eda38f1ae331516fd62527114514ec5920da6567c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f77707a61672f6c61726176656c2d71756572792d6275696c6465722f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/wpzag/laravel-query-builder/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/3066633bb6892cf5f4c540a56bd32cdf71e274f24ff3496b43995174420f231d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77707a61672f6c61726176656c2d71756572792d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wpzag/laravel-query-builder)

This package allows you to filter, sort, append, paginate and load relations based on the request query parameters.

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

[](#installation)

You can install the package via composer:

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

You can publish the config file with:

```
php artisan vendor:publish --tag="query-builder-config"
```

Usage
-----

[](#usage)

### Add config for your model in query-builder.php

[](#add-config-for-your-model-in-query-builderphp)

```
'models' => [
    User::class => [
       'includes' => ['posts.comments.author', ],
       'appends' => ['fullname','avatar'],
       'filterable' => ['*:except:role', 'posts.title', 'posts.body', 'posts.created_at'],
       'sortable' => ['*'],
       'max_per_page' => 20
     ],
 ]
```

#### \[ \* \] means all the model's attribute except hidden .

[](#---means-all-the-models-attribute-except-hidden-)

#### \[ \*:except:column1,column2 \] You can also exclude some attributes by adding :except: then comma separated attributes .

[](#--exceptcolumn1column2----you-can-also-exclude-some-attributes-by-adding-except-then-comma-separated-attributes-)

#### \[ age:exact \] If you prefixed the attribute with :extact it will use the ( = ) operator instead of ( LIKE ) operator in the query.

[](#--ageexact----if-you-prefixed-the-attribute-with-extact-it-will-use-the-----operator-instead-of--like--operator-in-the-query)

Usage
=====

[](#usage-1)

```
$builder = QueryBuilder::for(User:class); //This applies includes/filters/sorts to the query

$builder->query() // Returns the query

$builder->get() // Returns the eloquent collection

$builder->withPagination() // Applies pagination to the query

$builder->withAppends() // Applies appends to the query

$builder->withPaginationAndAppends() // Applies pagination & appends to the query
```

### You can also add custom query to query-builder

[](#you-can-also-add-custom-query-to-query-builder)

```
QueryBuilder::for(User:class)->query()->where('id', '>', 1)->get();

// Or if you are using  withPagination(),withAppends() or withPaginationAndAppends() :
QueryBuilder::for(User:class, function($query){

	//extra queries here

	return $next($query);
})->withPaginationAndAppends();
```

Examples:
=========

[](#examples)

### Filtering

[](#filtering)

```
users?filter[name]=john,rose

// User::where('name','like','%john%')
//       ->orWhere('name','like','%rose%')->get();
```

```
users?filter[name,username]=john

// User::where('name','like','%john%')
//       ->orWhere('username','like','%rose%')->get();
```

```
users?filter[name]=john,rose&filter[age]=gt.20

// User::where('name','like','%john%')
//       ->orWhere('name','like','%rose%')
//       ->where('age','>',20)->get();
```

```
users?filter[age][between]=[29,40]

// User::whereBetween('age', [29, 40])->get();
```

```
users?filter[email_verified_at][empty]

// User::whereNull('email_verified_at')->get();
```

```
users?filter[created_at][date]=2020-01-01

// User::whereDate('created_at', '2020-01-01')->get();
```

```
users?filter[posts.created_at]=lt.2020-01-01

// User::whereHas('posts', function($query) {
//     $query->whereDate('created_at', '
