Skip to content

FilterItTool/.github

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

FilterIt Tool

FilterIt is a tool that helps you parse and filter query strings easily. You can use it on both the front-end and back-end sides of your app.

Front-end Usage

On the front-end side of your app, you can use filterit-ts to build dynamic query strings that can be used to filter data on the server side. Here's a simple example of usage:

    const filter = new FilterIt();
    filter.Equal('id', 20).OrEqual('id', 22).SortBy('age');

    console.log(filter.toString()); // id=equal:20||equal:22&sort=age:desc

In this example, we're creating a new FilterIt object and adding some filter and sort criteria to it. The toString() method returns a formatted query string that can be used on the back-end side of your app.

Back-end Usage

On the back-end side of your app, you can use FilterIt to parse the query string generated by the front-end and use it to filter data in your database. Here's an example of how to use FilterIt in Laravel using filterit-laravel:

class UserController extends BaseController
{
    public function index(Request $request)
    {
        return User::filterit($request->query())->get();
    }
}

How it works?

You can use any of the following query parameters to filter and sort the results:

  • Filters: Specify filter conditions in the format < field | relation_field >=< operator >:< value >. Multiple filters can be chained together using the '||' (or) and ',' (and) symbols. Supported operators are:
    • equal, not_equal
    • like, not_like
    • ends_with, starts_with
    • greater_than, greater_than_equal
    • less_than, less_than_equal
    • in, not_in
    • between, not_between
    • is_null, not_null
    • relation filters: FilterIt also supports relationship filters like normal filters. Relationship fields must be connected with the character '___'. Some examples are:
      • posts___tags___title=equal:filterit
  • Sort: Specify the sort order in the format sort_by=< field | relation_field >:< direction >. Multiple sorts can be chained together using the ',' (and) symbol. Supported directions are 'asc' and 'desc'.

For example, to retrieve the users whose name contains "John", sorted by name:

"/api/users?name=like:John&id=not_in:(123)&sort=name:asc"

Back-end Frameworks

FilterIt is compatible with many back-end frameworks, including:

Releases

No releases published

Packages

No packages published