PHPackages                             laravie-update/query-filter - 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. laravie-update/query-filter

ActiveLibrary

laravie-update/query-filter
===========================

Database/Eloquent Query Builder filters for Laravel

3.0.x-dev(3y ago)023MITPHPPHP ^7.3 || ^8.0 || ^8.1

Since Oct 13Pushed 3y agoCompare

[ Source](https://github.com/SyedGhazanfar/query-filter)[ Packagist](https://packagist.org/packages/laravie-update/query-filter)[ Fund](https://paypal.me/crynobone)[ Fund](https://liberapay.com/crynobone)[ RSS](/packages/laravie-update-query-filter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

Database/Eloquent Query Builder filters for Laravel
===================================================

[](#databaseeloquent-query-builder-filters-for-laravel)

[![tests](https://github.com/laravie/query-filter/workflows/tests/badge.svg?branch=3.x)](https://github.com/laravie/query-filter/actions?query=workflow%3Atests+branch%3A3.x)[![Latest Stable Version](https://camo.githubusercontent.com/772fc8dc7b58afe5757116df3aedd3974f53bb1ad0db26209518bcf97253c394/68747470733a2f2f706f7365722e707567782e6f72672f6c6172617669652f71756572792d66696c7465722f762f737461626c65)](https://packagist.org/packages/laravie/query-filter)[![Total Downloads](https://camo.githubusercontent.com/3f458eb48ce1b93fca3c6c852247ca189bc9e99b8c301c097b08dd02ac3d0bbf/68747470733a2f2f706f7365722e707567782e6f72672f6c6172617669652f71756572792d66696c7465722f646f776e6c6f616473)](https://packagist.org/packages/laravie/query-filter)[![Latest Unstable Version](https://camo.githubusercontent.com/f41875d5d456d2b174c6f0f17088743a9139993bdd812d779ee1f04352af84b4/68747470733a2f2f706f7365722e707567782e6f72672f6c6172617669652f71756572792d66696c7465722f762f756e737461626c65)](https://packagist.org/packages/laravie/query-filter)[![License](https://camo.githubusercontent.com/e650c6f78dca4030d56e8be24d019095ee790f060866fc723fb21177a6436772/68747470733a2f2f706f7365722e707567782e6f72672f6c6172617669652f71756572792d66696c7465722f6c6963656e7365)](https://packagist.org/packages/laravie/query-filter)[![Coverage Status](https://camo.githubusercontent.com/5ac00bb635c318df603db28080ad9ead72bebedaf5ee752696a6b129622346be/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6c6172617669652f71756572792d66696c7465722f62616467652e7376673f6272616e63683d332e78)](https://coveralls.io/github/laravie/query-filter?branch=3.x)

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

[](#installation)

To install through composer, run the following command from terminal:

```
composer require "laravie/query-filter"

```

Usages
------

[](#usages)

### Order Queries

[](#order-queries)

```
new Laravie\QueryFilter\Orderable(?string $column, string $direction = 'asc', array $config = []);
```

The class provides a simple interface to handle `ORDER BY` queries to Laravel Eloquent/Query Builder.

```
use App\User;
use Laravie\QueryFilter\Orderable;

$query = User::query();

$orderable = new Orderable(
    'name', 'desc'
);

return $orderable->apply($query)->get();
```

```
select * from `users` order by `name` desc;
```

> The code will validate the column name before trying to apply `orderBy()` to the query, this would prevent SQL injection especially when column is given by the user.

### Search Queries

[](#search-queries)

```
new Laravie\QueryFilter\Searchable(?string $keyword, array $columns = []);
```

The class provides a simple interface to `LIKE` queries to Laravel Eloquent/Query Builder.

```
use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'crynobone', ['name', 'email']
);

return $searchable->apply($query)->get();
```

```
select * from `users`
where (
    (
        `name` like 'crynobone'
        or `name` like 'crynobone%'
        or `name` like '%crynobone'
        or `name` like '%crynobone%'
    ) or (
        `email` like 'crynobone'
        or `email` like 'crynobone%'
        or `email` like '%crynobone'
        or `email` like '%crynobone%'
    )
);
```

#### Search with wildcard

[](#search-with-wildcard)

Set specific `%` or `*` wildcard to reduce the possible `LIKE`s variations.

```
use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'crynobone*gmail', ['name', 'email']
);

return $searchable->apply($query)->get();
```

```
select * from `users`
where (
    (
        `name` like 'crynobone%gmail'
    ) or (
        `email` like 'crynobone%gmail'
    )
);
```

#### Search with exact wildcard

[](#search-with-exact-wildcard)

Use `noWildcardSearching()` to disable adding additional search condition.

```
use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = (new Searchable(
    'crynobone@gmail', ['name', 'email']
))->noWildcardSearching();

return $searchable->apply($query)->get();
```

```
select * from `users`
where (
    (
        `name` like 'crynobone@gmail'
    ) or (
        `email` like 'crynobone@gmail'
    )
);
```

#### Search with JSON path

[](#search-with-json-path)

This would allow you to query JSON path using `LIKE` with case insensitive.

```
use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'Malaysia', ['address->country']
);

return $searchable->apply($query)->get();
```

```
select * from `users`
where (
    (
        lower(json_unquote(json_extract(`meta`, '$."country"'))) like 'malaysia'
        or lower(json_unquote(json_extract(`meta`, '$."country"'))) like 'malaysia%'
        or lower(json_unquote(json_extract(`meta`, '$."country"'))) like '%malaysia'
        or lower(json_unquote(json_extract(`meta`, '$."country"'))) like '%malaysia%'
    )
);
```

#### Search with Relations

[](#search-with-relations)

This would make it easy to search results not only in the current model but also it's relations.

```
use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'Administrator', ['name', 'roles.name']
);

return $searchable->apply($query)->get();
```

```
select * from `users`
where (
    (
        `name` like 'Administrator'
        or `name` like 'Administrator%'
        or `name` like '%Administrator'
        or `name` like '%Administrator%'
    ) or exists (
        select * from `roles`
        inner join `user_role`
            on `roles`.`id` = `user_role`.`role_id`
        where `users`.`id` = `user_role`.`user_id`
        and (
            `name` like 'Administrator'
            or `name` like 'Administrator%'
            or `name` like '%Administrator'
            or `name` like '%Administrator%'
        )
    )
);
```

> Relations search can only be applied to `Illuminate\Database\Eloquent\Builder` as it need to ensure that the relationship exists via `whereHas()` queries.

#### Search with Morph Relations

[](#search-with-morph-relations)

You can use polymorphic relationship search using the following options:

```
use App\Comment;
use Laravie\QueryFilter\Searchable;
use Laravie\QueryFilter\Filters\MorphRelationSearch;

$query = Comment::query();

$searchable = new Searchable(
    'Administrator', ['name', new MorphRelationSearch('commentable', 'name')]
);

return $searchable->apply($query)->get();
```

### Taxonomy Queries

[](#taxonomy-queries)

```
new Laravie\QueryFilter\Taxonomy(?string $keyword, array $rules, array $columns = []);
```

Taxonomy always developers to create a set of rules to group the search keywords using `WHERE ... AND`. For any un-grouped keyword it will be executed via `Laravie\QueryFilter\Searchable` based on given `$columns`.

```
use App\User;
use Laravie\QueryFilter\Taxonomy;

$query = User::query();

$taxonomy = new Taxonomy(
    'is:admin email:crynobone@gmail.com', [
        'email:*' => static function ($query, $value) {
            return $query->where('email', '=', $value);
        },
        'role:[]' => static function ($query, array $value) {
            return $query->whereIn('role', $value);
        },
        'is:admin' => static function ($query) {
            return $query->where('admin', '=', 1);
        },
    ],
);

$taxonomy->apply($query)->get();
```

```
select * from `user`
where `email`='crynobone@gmail.com'
and `admin`=1;
```

Integrations
------------

[](#integrations)

### Query Builder Macro

[](#query-builder-macro)

You can integrate `Searchable` with database or eloquent query builder macro by adding the following code to your `AppServiceProvider` (under `register` method):

```
