PHPackages                             faridlab/laravel-search-query - 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. [Search &amp; Filtering](/categories/search)
4. /
5. faridlab/laravel-search-query

ActiveLibrary[Search &amp; Filtering](/categories/search)

faridlab/laravel-search-query
=============================

Search Query - Laravel package for filtering queries based on url query string parameters

v9.0.16(1y ago)1170116MITPHPPHP &gt;=7.1

Since Jul 30Pushed 1y agoCompare

[ Source](https://github.com/faridlab/laravel-search-query)[ Packagist](https://packagist.org/packages/faridlab/laravel-search-query)[ Docs](https://github.com/faridlab/laravel-search-query)[ RSS](/packages/faridlab-laravel-search-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (26)Used By (16)

Laravel Filter Query String
===========================

[](#laravel-filter-query-string)

#### Filter your queries based on url query string parameters like a breeze.

[](#filter-your-queries-based-on-url-query-string-parameters-like-a-breeze)

*Compatible with Laravel **5.x** **6.x** **7.x** **8.x** **9.x** **10.x** **11.x***.

Table of Content
----------------

[](#table-of-content)

- [Describing the Problem](#Describing-the-Problem)
- [Usage](#Usage)
    - [Installation](#Usage)
    - [Available Filters](#Available-Methods)
        - [Sort](#Sort)
        - [Comparisons](#Comparisons)
        - [In](#In)
        - [Like](#Like)
        - [ILike](#ILike)
        - [Where clause](#Where-Clause-Default-Filter)
        - [Fields](#fields)
        - [Search](#search)
        - [Page](#page)
        - [Limit](#limit)
        - [Relationship](#relationship)
        - [Count](#count)
        - [With Trashed](#withtrashed)
        - [Order By](#orderby)
        - [Where](#where)
        - [Or Where](#or-where)
        - [Equal](#equal)
        - [Not Equal](#not-equal)
        - [Greater Than](#greater-than)
        - [Greater Than Equal](#greater-than-equal)
        - [Less Than](#less-than)
        - [Less Than Equal](#less-than-equal)
        - [Like](#like)
        - [Not Like](#not-like)
        - [Contain](#contain)
        - [Not Contain](#not-contain)
        - [Start With](#start-with)
        - [End With](#end-with)
        - [In](#in)
        - [Not In](#not-in)
        - [Between](#between)
        - [Not Between](#not-between)
        - [Is Null](#is-null)
        - [Is Not Null](#is-not-null)
        - [Distinct](#distinct)
    - [Custom Filters](#Custom-Filters)
    - [Conditional Filters](#Conditional-Filters)
    - [Manually Passing Filter Array (Livewire)](#Manually-Passing-Filter-Array-Livewire)

Describing the Problem
----------------------

[](#describing-the-problem)

You have probably faced the situation where you needed to filter your query based on given parameters in url query-string and after developing the logics, You've had such a code:

```
$users = User::latest();

if(request('username')) {
    $users->where('username', request('username'));
}

if(request('age')) {
    $users->where('age', '>', request('age'));
}

if(request('email')) {
    $users->where('email', request('email'));
}

return $users->get();
```

This works, But it's not a good practice.

When the number of parameters starts to grow, The number of these kind of `if` statements also grows and your code gets huge and hard to maintain.

Also it's against the Open/Closed principal of SOLID principles, Because when you have a new parameter, You need to get into your existing code and add a new logic (which may breaks the existing implementations).

So we have to design a way to make our filters logics separated from each other and apply them into the final query, which is the whole idea behind this package.

Usage
-----

[](#usage)

1. First you need to install the package:

`$ composer require faridlab/laravel-grammatical-query`

2. Then you should `use` the `FilterQueryString` trait in your model, And define `$filters` property which can be consist of [available filters](#Available-Methods) or your [custom filters](#custom-filters).

```
use SearchQuery\FilterQueryString\FilterQueryString;

class User extends Model
{
    use FilterQueryString;

    protected $filters = [];

    ...
}
```

3. You need to use `filter()` method in your eloquent query. For example:

```
User::select('name')->filter()->get();
```

### Available Methods

[](#available-methods)

- [Sort](#Sort)
- [Comparisons](#Comparisons)
- [In](#In)
- [Like](#Like)
- [Where clause](#Where-Clause-Default-Filter)

**Filters**:

- [fields](#fields): `array ― optional`
- [search](#search): `string ― optional`
- [page](#page): `integer default(1) ― optional`
- [limit](#limit): `integer default(25) ― optional`
- [relationship](#relationship): `array ― optional`
- [count](#count): `array ― optional`
- [withtrashed](#withtrashed): `boolean default(false) ― optional`
- [orderby](#orderby): `array ― optional`
- [fieldname\[where\]](#where): `string|array ― optional`
- [fieldname\[orwhere\]](#or-where): `string|array ― optional`
- [fieldname\[eq\]](#equal): `string|integer ― optional`
- [fieldname\[notEq\]](#not-equal): `string|integer ― optional`
- [fieldname\[gt\]](#greater-than): `string|integer ― optional`
- [fieldname\[gtEq\]](#greater-than-equal): `string|integer ― optional`
- [fieldname\[lt\]](#less-than): `string|integer ― optional`
- [fieldname\[ltEq\]](#less-than-equal): `string|integer ― optional`
- [fieldname\[like\]](#like): `string ― optional`
- [fieldname\[notlike\]](#not-like): `string ― optional`
- [fieldname\[contain\]](#contain): `string ― optional`
- [fieldname\[notcontain\]](#not-contain): `string ― optional`
- [fieldname\[startwith\]](#start-with): `string ― optional`
- [fieldname\[endwith\]](#end-with): `string ― optional`
- [fieldname\[in\]](#in): `array ― optional`
- [fieldname\[notin\]](#not-in): `array ― optional`
- [fieldname\[between\]](#between): `array ― optional`
- [fieldname\[notbetween\]](#not-between): `array ― optional`
- [fieldname\[isnull\]](#is-null): `string ― optional`
- [fieldname\[isnotnull\]](#is-not-null): `string ― optional`

QueryParameterNoteSQLAND(where)WHERE and...WHERE 1 = 1 *AND fieldname = {search}*...OR(orwhere)WHERE or...WHERE 1 = 1 *OR fieldname = {search}*...=(eq)EQual...WHERE *fieldname = {search}*...&gt;(gt)Greater Than...WHERE *fieldname &gt; {search}*...&gt;=(gtEq)Greater Than EQual...WHERE *fieldname &gt;= {search}*...&lt;(lt)Less Than...WHERE *fieldname &lt; {search}*...&lt;=(ltEq)Less Than EQual...WHERE *fieldname &lt;= {search}*...!=(notEq)NOT EQual...WHERE *fieldname != {search}*...LIKE(like)LIKE...WHERE *fieldname LIKE {search}*...LIKE %...%(contain)LIKE %...%...WHERE *fieldname LIKE %{search}%*...LIKE startwith(startwith)LIKE startwith%...WHERE *fieldname LIKE {search}%*...LIKE %endwith(endwith)LIKE %endwith...WHERE *fieldname LIKE %{search}*...NOT LIKE(notlike)NOT LIKE...WHERE *fieldname NOT LIKE {search}*...IN (...)(in)IN...WHERE *fieldname IN({search})*...NOT IN (...)(notin)NOT IN...WHERE *fieldname NOT IN({search})*...BETWEEN(between)BETWEEN...WHERE *fieldname BETWEEN {search} AND {search}*...NOT BETWEEN(notbetween)NOT BETWEEN...WHERE *fieldname NOT BETWEEN {search} AND {search}*...IS NULL(isnull)IS NULL...WHERE *fieldname IS NULL*...IS NOT NULL(isnotnull)IS NOT NULL...WHERE *fieldname IS NOT NULL*...DISTINCT(distinct)DISTINCT...WHERE *DISTINCT(fieldname)*...ORDER BY(orderby)ORDER BY...ORDER BY *fieldname {orderby}*...For the purpose of explaining each method, Imagine we have such data in our `users` table:

idnameemailusernameagecreated\_at1mehradmehrad@startapp.idmehrad123202020-09-012rezareza@startapp.idreza123202020-10-013hosseinhossein@startapp.idhossein123222020-11-014dariushdariush@startapp.iddariush123222020-12-015faridfarid@startapp.idfaridlab212021-03-12And assume our query is something like this:

```
User::filter()->get();
```

### Fields

[](#fields)

```
fields: array|string ― optional

```

Convention:

```
> GET /api/v1/users?fields={fieldname}
> GET /api/v1/users?fields[]={fieldname1}
> GET /api/v1/users?fields[]={fieldname2}

> GET /api/v1/users?fields=name
> GET /api/v1/users?fields[]=name&fields[]=email

```

In Users.php

```
protected $filters = ['fields'];
```

**Example**: `https://startapp.id/api/v1/users?fields[]=name&fields[]=email`

### Search

[](#search)

```
search: string ― optional

```

Convention:

```
> GET /api/v1/users?search={fieldname}

> GET /api/v1/users?search=faridlab

```

In Users.php

```
protected $filters = ['search'];
```

**Example**: `https://startapp.id/api/v1/users?search=faridlab`

idnameemailusernameagecreated\_at5faridfarid@startapp.idfaridlab212021-03-12Note: please more specific to defined what field you want to search, this filter not recommend in production, because this filter will search all fields, and make your app perf slower.

### Page

[](#page)

```
page: integer default(1) ― optional

```

Convention:

```
> GET /api/v1/users?page={page}

> GET /api/v1/users?page=1

```

In Users.php

```
protected $filters = ['page'];
```

**Example**: `https://startapp.id/api/v1/users?page=1`

### Limit

[](#limit)

```
limit: integer default(25) ― optional

```

Convention:

```
> GET /api/v1/users?limit={limit}

> GET /api/v1/users?limit=25

```

In Users.php

```
protected $filters = ['limit'];
```

**Example**: `https://startapp.id/api/v1/users?limit=25`

### Relationship

[](#relationship)

```
relationship: array|string ― optional

```

Convention:

```
> GET /api/v1/users?relationship={relation}
> GET /api/v1/users?relationship[]={relation1}
> GET /api/v1/users?relationship[]={relation2}

> GET /api/v1/users?relationship=role
> GET /api/v1/users?relationship[]=role
> GET /api/v1/users?relationship[]=permissions

> GET /api/v1/users?relationship[role]={search} # search for role id as default field value
> GET /api/v1/users?relationship[role][name]={search} # search for role field `name`

```

In Users.php

```
protected $filters = ['relationship'];
```

**Example**: `https://startapp.id/api/v1/users?relationship=role`

`https://startapp.id/api/v1/users?relationship[]=role&relationship[]=permissions`

### Count

[](#count)

```
count: array|string ― optional

```

Convention:

```
> GET /api/v1/users?count={relation}
> GET /api/v1/users?count[]={relation1}
> GET /api/v1/users?count[]={relation2}

> GET /api/v1/users?count=addresses
> GET /api/v1/users?count[]=photos
> GET /api/v1/users?count[]=accounts

```

In Users.php

```
protected $filters = ['count'];
```

**Example**: `https://startapp.id/api/v1/users?count=addresses`

`https://startapp.id/api/v1/users?count[]=photos&count[]=accounts`

### Withtrashed

[](#withtrashed)

```
withtrashed: boolean default(false) ― optional

```

Convention:

```
> GET /api/v1/users?withtrashed=true

> GET /api/v1/users?withtrashed=true

```

In Users.php

```
protected $filters = ['withtrashed'];
```

**Example**: `https://startapp.id/api/v1/users?withtrashed=true`

### Orderby

[](#orderby)

```
orderby: array|string ― optional

```

Orderby is the equivalent to `order by` sql statement which can be used flexible in `FilterQueryString`:

Conventions:

```
> GET /api/v1/users?orderby={fieldname}
> GET /api/v1/users?orderby[{fieldname}]={asc|desc}&orderby[{fieldname}]={asc|desc}

> GET /api/v1/users?orderby=name
> GET /api/v1/users?orderby[id]=desc&orderby[name]=asc
> GET /api/v1/users?orderby[id]=desc&orderby[name]=asc&orderby[email]=asc
```

In Users.php

```
protected $filters = ['orderby'];
```

**Single `sort`**:

`https://startapp.id/api/v1/users?orderby=created_at`

Output:

nameemailusernameagecreated\_atmehradmehrad@startapp.idmehrad123202020-09-01rezareza@startapp.idreza123202020-10-01hosseinhossein@startapp.idhossein123222020-11-01dariushdariush@startapp.iddariush123222020-12-01faridfarid@startapp.idfaridlab212021-03-12- **Note** that when you're not passing parameter as array instead of string, it will be used as field name and order by 'asc' by default.

**Multiple `sort`s**:

`https://startapp.id/api/v1/users?orderby[name]=asc&orderby[email]=desc`

Output:

nameemailusernameagecreated\_atdariushdariush@startapp.iddariush123222020-12-01faridfarid@startapp.idfaridlab212021-03-12hosseinhossein@startapp.idhossein123222020-11-01mehradmehrad@startapp.idmehrad123202020-09-01rezareza@startapp.idreza123202020-10-01**Bare in mind** that `orderby` parameter with invalid values will be ignored from query and has no effect to the result.

### Where

[](#where)

```
fieldname[where]: string|array ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}={searchtext}
> GET /api/v1/users?{fieldname}[]={searchtext}
> GET /api/v1/users?{fieldname}[where]={searchtext}

> GET /api/v1/users?username=faridlab
> GET /api/v1/users?username[]=faridlab
> GET /api/v1/users?username[where]=faridlab

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username=faridlab`

`https://startapp.id/api/v1/users?username[]=faridlab`

`https://startapp.id/api/v1/users?username[where]=faridlab`

### Or where

[](#or-where)

```
fieldname[orwhere]: string|array ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[orwhere]={searchtext}
> GET /api/v1/users?{fieldname}[orwhere][]={searchtext}
> GET /api/v1/users?{fieldname}[orwhere][]={searchtext}

> GET /api/v1/users?username[where]=faridlab
> GET /api/v1/users?username[where][]=faridlab
> GET /api/v1/users?username[where][]=mehrad123

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[where]=faridlab`

`https://startapp.id/api/v1/users?username[where][]=faridlab`

`https://startapp.id/api/v1/users?username[where][]=mehrad123`

### Equal

[](#equal)

```
fieldname[eq]: string|integer ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[eq]={searchtext}
> GET /api/v1/users?{fieldname2}[eq]={searchtext}

> GET /api/v1/users?username[eq]=faridlab&email[eq]=farid@startapp.id

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[eq]=faridlab&email[eq]=farid@startapp.id`

### Not Equal

[](#not-equal)

```
fieldname[notEq]: string|integer ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[notEq]={searchtext}
> GET /api/v1/users?{fieldname2}[notEq]={searchtext}

> GET /api/v1/users?username[notEq]=faridlab&email[notEq]=farid@startapp.id

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[notEq]=faridlab&email[notEq]=farid@startapp.id`

### Greater Than

[](#greater-than)

```
fieldname[gt]: string|integer ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[gt]={searchtext}

> GET /api/v1/users?id[gt]=10

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?id[gt]=10`

### Greater Than Equal

[](#greater-than-equal)

```
fieldname[gtEq]: string|integer ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[gtEq]={searchtext}

> GET /api/v1/users?id[gtEq]=10

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?id[gtEq]=10`

### Less Than

[](#less-than)

```
fieldname[lt]: string|integer ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[lt]={searchtext}

> GET /api/v1/users?id[lt]=10

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?id[lt]=10`

### Less Than Equal

[](#less-than-equal)

```
fieldname[ltEq]: string|integer ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[ltEq]={searchtext}

> GET /api/v1/users?id[ltEq]=10

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?id[ltEq]=10`

### Like

[](#like)

```
fieldname[like]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[like]={searchtext}

> GET /api/v1/users?username[like]=faridlab

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[like]=faridlab`

### ILike

[](#ilike)

```
fieldname[ilike]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[ilike]={searchtext}

> GET /api/v1/users?username[ilike]=faridlab

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[ilike]=faridlab`

### Not Like

[](#not-like)

```
fieldname[notlike]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[notlike]={searchtext}

> GET /api/v1/users?username[notlike]=faridlab

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[notlike]=faridlab`

### Contain

[](#contain)

```
fieldname[contain]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[contain]={searchtext}

> GET /api/v1/users?username[contain]=farid

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[contain]=farid`

### Not Contain

[](#not-contain)

```
fieldname[notcontain]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[notcontain]={searchtext}

> GET /api/v1/users?username[notcontain]=farid

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[notcontain]=farid`

### Start With

[](#start-with)

```
fieldname[startwith]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[startwith]={searchtext}

> GET /api/v1/users?username[startwith]=farid

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[startwith]=farid`

### End With

[](#end-with)

```
fieldname[endwith]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[endwith]={searchtext}

> GET /api/v1/users?username[endwith]=lab

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[endwith]=lab`

### In

[](#in)

```
fieldname[in]: array ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[in][]={searchtext}&{fieldname}[in][]={searchtext2}

> GET /api/v1/users?username[in][]=faridlab&username[in][]=farid

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[in][]=faridlab&username[in][]=farid`

### Not In

[](#not-in)

```
fieldname[notin]: array ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[notin][]={searchtext}&{fieldname}[notin][]={searchtext2}

> GET /api/v1/users?username[notin][]=faridlab&username[notin][]=farid

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?username[notin][]=faridlab&username[notin][]=farid`

### Between

[](#between)

```
fieldname[between]: array ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[between][]={searchtext}&{fieldname}[between][]={searchtext2}

> GET /api/v1/users?id[between][]=1&id[between][]=10

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?id[between][]=1&id[between][]=10`

### Not Between

[](#not-between)

```
fieldname[notbetween]: array ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[notbetween][]={searchtext}&{fieldname}[notbetween][]={searchtext2}

> GET /api/v1/users?id[notbetween][]=1&id[notbetween][]=10

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?id[notbetween][]=1&id[notbetween][]=10`

### Is Null

[](#is-null)

```
fieldname[isnull]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[isnull]={null|''}

> GET /api/v1/users?deleted_at[isnull]=null

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?deleted_at[isnull]=null`

`https://startapp.id/api/v1/users?deleted_at[isnull]`

### Is Not Null

[](#is-not-null)

```
fieldname[isnotnull]: string ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[isnotnull]={null|''}

> GET /api/v1/users?deleted_at[isnotnull]=null

```

In Users.php

```
protected $filters = [];
```

**Example**: `https://startapp.id/api/v1/users?deleted_at[isnotnull]=null`

`https://startapp.id/api/v1/users?deleted_at[isnotnull]`

### Distinct

[](#distinct)

```
fieldname[distinct]: boolean ― optional

```

Convention:

```
> GET /api/v1/users?{fieldname}[distinct]={true|1}

> GET /api/v1/users?first_name[distinct]=true

```

In Users.php

```
protected $filters = ['distinct'];
```

**Example**: `https://startapp.id/api/v1/users?first_name[distinct]=true`

`https://startapp.id/api/v1/users?first_name[distinct]=1`

`https://startapp.id/api/v1/users?first_name[distinct]`

### Where Clause (default filter)

[](#where-clause-default-filter)

Generally when your query string parameters are not one of previous available methods, It'll get filtered by the default filter which is the `where` sql statement. It's the proper filter when you need to directly filter one of your table's columns.

Conventions:

```
?field=value
?field1=value&field2=value
?field1[0]=value1&field1[1]=value2
?field1[0]=value1&field1[1]=value2&field2[0]=value1&field2[1]=value2

```

Assuming we want to filter `name`, `username` and `age` database columns, In Users.php

```
protected $filters = ['name', 'username', 'age'];
```

**Example**:

`https://startapp.id?name=mehrad`

Output:

nameemailusernameagecreated\_atmehradmehrad@startapp.idmehrad123202020-09-01**Example**:

`https://startapp.id?age=22&username=dariush123`

Output:

nameemailusernameagecreated\_atdariushdariush@startapp.iddariush123222020-12-01**Example**:

`https://startapp.id?name[0]=mehrad&name[1]=dariush`

Output:

nameemailusernameagecreated\_atmehradmehrad@startapp.idmehrad123202020-09-01dariushdariush@startapp.iddariush123222020-12-01**Example**:

`https://startapp.id?name[0]=mehrad&name[1]=dariush&username[0]=mehrad123&username[1]=reza1234`

Output:

nameemailusernameagecreated\_atmehradmehrad@startapp.idmehrad123202020-09-01**Bare in mind** that `default` filter parameter with invalid values will be ignored from query and has no effect to the result.

### Custom Filters

[](#custom-filters)

By custom filters you can define your own methods as filters. This helps with the Open/Closed of SOLID principles, Hence each time a new filter is needed, you don't have to edit previous filters and you can just write a separate method for it.

Let's create a custom filter. Assuming you want to create a filter named `all_except` which retrieves all users except the one that is specified:

In Users.php

```
protected $filters = ['all_except'];

public function all_except($query, $value) {
    return $query->where('name', '!=', $value);
}
```

To test our newly added filter:

`https://startapp.id?all_except=mehrad`

Output:

nameemailusernameagecreated\_atrezareza@startapp.idreza123202020-10-01hosseinhossein@startapp.idhossein123222020-11-01dariushdariush@startapp.iddariush123222020-12-01**Note** that your custom defined filters have the most priority which means you can even override available filters.

For example lets change `in` filter in a way that only accepts 3 values:

In Users.php

```
protected $filters = ['in'];

public function in($query, $value) {

    $exploded = explode(',', $value);

    if(count($exploded) != 4) {
        // throwing an exception or whatever you like to do
    }

    $field = array_shift($exploded);

    return $query->whereIn($field, $exploded);
}
```

**Another** good example for custom filters are when you don't want to expose your database table's column name. For example assume we don't want to expose that we have a column named `username` in `users` table:

In Users.php

```
protected $filters = ['by'];

public function by($query, $value) {
    return $query->where('username', $value);
}
```

`https://startapp.id?by=dariush123`

Output:

nameemailusernameagecreated\_atdariushdariush@startapp.iddariush123222020-12-01#### Minor Tip

[](#minor-tip)

In order to prevent your model to get messy or populated with filter methods, You can create a trait for it and put everything about filters inside the trait.

### Conditional Filters

[](#conditional-filters)

The `$filters` property in your model is acting kind of global for that model. It means when you use `filter()` method on your eloquent query, it'll always performs all the `$filters` filters.

There might be situations that based on a condition you need to specify which filters exactly you wish to be filtered.

To achieve this you can specify your desired filters as arguments in `filter()` method.

Example:

In your query:

```
User::filter('in')->get();
```

`in=name,mehrad,reza&like=name,mehrad`

Output:

nameemailusernameagecreated\_atmehradmehrad@startapp.idmehrad123202020-09-01rezareza@startapp.idreza123202020-10-01If the `in` argument was not specified, The result of query would be only one record (`mehrad`).

Another example:

In your query:

```
User::filter('like', 'name')->get();
```

`like=name,mehrad,reza,dariush,hossein&name[0]=mehrad&name[1]=hossein&username=mehrad`

Output:

nameemailusernameagecreated\_atmehradmehrad@startapp.idmehrad123202020-09-01hosseinhossein@startapp.idhossein123222020-11-01### Manually Passing Filter Array (Livewire)

[](#manually-passing-filter-array-livewire)

When using Livewire to filter data, subsequent query string changes do not trigger new requests. We can work around this by manually passing an array of filters.

Example:

```
User::filter(['username' => 'mehrad123'])->get();
```

Another example:

```
User::filter([
    'username' => [
        'contain' => 'medhrad',
    ],
    'email' => [
        'contain' => 'startapp.id',
     ]
])->get();
```

You can also combine this with conditional filters:

```
User::filter([
    'username' => 'mehrad123',
    'email' => [
        'contains' => 'startapp.id'
    ]
], 'username')->get();
```

The above would only query the username (not the email) since only the username was included as a conditional.

**Note that the filter array must be passed before the conditionals.**

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance45

Moderate activity, may be stable

Popularity13

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 59.6% 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 ~67 days

Recently: every ~6 days

Total

25

Last Release

503d ago

Major Versions

v1.1.6 → v2.02021-03-18

v2.2.0 → v9.0.02022-07-15

PHP version history (3 changes)v1.0.0PHP ^7.1

v1.1.6PHP &gt;=7.1

v9.0.0PHP ^8.0.2

### Community

Maintainers

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

---

Top Contributors

[![faridlab](https://avatars.githubusercontent.com/u/1629524?v=4)](https://github.com/faridlab "faridlab (90 commits)")[![mehradsadeghi](https://avatars.githubusercontent.com/u/31504728?v=4)](https://github.com/mehradsadeghi "mehradsadeghi (56 commits)")[![archilex](https://avatars.githubusercontent.com/u/6097099?v=4)](https://github.com/archilex "archilex (5 commits)")

---

Tags

querystringquery-stringqueryfilterfilter-querystringfilter-query-string

### Embed Badge

![Health badge](/badges/faridlab-laravel-search-query/health.svg)

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

###  Alternatives

[mehradsadeghi/laravel-filter-querystring

Filter your queries based on url query string parameters like a breeze.

169118.2k](/packages/mehradsadeghi-laravel-filter-querystring)[suenerds/nova-searchable-belongs-to-filter

Searchable Nova filter for belongsTo relationships.

29516.9k](/packages/suenerds-nova-searchable-belongs-to-filter)[ambengers/query-filter

Laravel package for filtering resources with request query string

3513.5k](/packages/ambengers-query-filter)[clue/json-query

JSON query language filter in PHP

3015.7k](/packages/clue-json-query)[pos-lifestyle/laravel-nova-date-range-filter

A Laravel Nova date range filter.

16179.1k](/packages/pos-lifestyle-laravel-nova-date-range-filter)[hashemi/queryfilter

A simple &amp; dynamic package for your eloquent query in laravel. It will help you to write query logic individual for each parameter.

391.1k](/packages/hashemi-queryfilter)

PHPackages © 2026

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