PHPackages                             youcanshop/queryoption - 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. youcanshop/queryoption

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

youcanshop/queryoption
======================

Package to help with filtering/search/sort in queries

v0.1.3(3y ago)714.5k↓23.7%1MITPHPPHP ^7.4 || ^8.0

Since Feb 26Pushed 2y ago2 watchersCompare

[ Source](https://github.com/youcan-shop/QueryOption)[ Packagist](https://packagist.org/packages/youcanshop/queryoption)[ RSS](/packages/youcanshop-queryoption/feed)WikiDiscussions main Synced 1mo ago

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

[![Query Option package logo](https://github.com/youcan-shop/QueryOption/raw/main/assets/queryoptionlogo.svg)](https://github.com/youcan-shop/QueryOption/blob/main/assets/queryoptionlogo.svg)
**QueryOption**

[![Tests](https://github.com/youcan-shop/QueryOption/actions/workflows/tests.yaml/badge.svg)](https://github.com/youcan-shop/QueryOption/actions/workflows/tests.yaml)[![Total Downloads](https://camo.githubusercontent.com/27a8f0b41da4f63f7199b0a6798a8957ecf73070ade76437479ad211fdd2ce56/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796f7563616e73686f702f71756572796f7074696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/youcanshop/queryoption)[![License](https://camo.githubusercontent.com/a5f8fa0065d3ca5fdf7e6f5546e00e69056c4bd53480d759f73506b5988eca6e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f796f7563616e2d73686f702f51756572794f7074696f6e3f7374796c653d666c61742d737175617265)](https://github.com/youcan-shop/QueryOption/blob/master/LICENSE.md)

This package helps you manipulate HTTP query data as an object instead of passing an array through different layers of your application.

Usage
-----

[](#usage)

Inside a controller we tend to extract the params from the request and send them to our service and then to the repository to perform search, sort or filtering.

```
class ListUsersController as Controller
{
    private UserService $userService;

    public function __construct(UserService $userService) {
        $this->userService = $userService;
    }

    public function __invoke(Request $request) {
        $queryOption = QueryOptionFactory::createFromIlluminateRequest($request);

        $this->userService->paginate($queryOption);
    }
}
```

From the example above, the `QueryOptionFactory` helps create the `QueryOption` object that hold values needed for search, sort and filters. Since in this example we have a Laravel application, we can use a specific factory method to crate directly from `Request` object.

`createFromGlobals()`create the `QueryOption` object from the global `$_REQUEST` object.`createFromArray(array $attributes)`create the `QueryOption` object from an array passed in param.`createFromIlluminateRequest(Request $request)`create the `QueryOption` object from a Laravel Illuminate request object.`createFromSymfonyRequest(Request $request)`create the `QueryOption` object from a Symfony HTTP foundation request object.URL Params
----------

[](#url-params)

Since the QueryOption package parse the URL parameters, we're going to explain the param names below:

`q`Used for search`search_type`(`like`: default, `equal`)`page`Current page (when working with pagination)`limit`Limit query result (when working with pagination)`sort_field`Name of the field when sorting (`created_at`: default)`sort_order`Sorting direction. (`asc`, `desc`: default)`filters`An array of filters (as described below)`field`Name of the field to filter by`operator`Comparison operator (`=`: default, `!=`, `>`, ``, `getSort();
$queryOption->getFilters();
$queryOption->getSearch();
```

For most applications, each controller has a set of filters that are allowed in certain context (admin vs normal user). In this case, you can use the `allowedFilters()` method inside the controller to limit passing filters in the wrong context.

An example would be listing blog posts. The admin can all the posts, while the normal user can only see the published ones.

```
class AdminPostsController {
    private PostService $postService;

    public function __construct(PostService $postService) {
        $this->postService = $postService;
    }

    public function __invoke(Request $request) {
        $queryOption = QueryOptionFactory::createFromIlluminateRequest($request);

        $posts = $this->postService->paginate($queryOption);

        // return posts
    }
}
```

```
class UserPostsController {
    private PostService $postService;

    public function __construct(PostService $postService) {
        $this->postService = $postService;
    }

    public function __invoke(Request $request) {
        $queryOption = QueryOptionFactory::createFromIlluminateRequest($request);

        // explicitly specify the filter names allowed.
        $queryOption->allowedFilters(['published_date', 'author']);

        $posts = $this->postService->paginate($queryOption);

        // return posts
    }
}
```

Laravel Bridge
--------------

[](#laravel-bridge)

For Laravel applications, you can add Query Option provider inside `config/app.php`

```
