Correct way of adding filter to Laravel 10
Tags: laravel, php, sql, api
Published: 2023-03-04 15:34:01

If you are working with Laravel, you probably know how powerful and expressive its Eloquent ORM is. Eloquent allows you to interact with your database using fluent and intuitive syntax. However, sometimes you may need to apply complex filtering and sorting logic to your data based on user input. For example, you may want to filter products by price range, category, rating, color, etc. or sort them by name, popularity, date, etc.

This is where [Laravel Purity](https://abbasudo.github.io/laravel-purity/) comes in handy. Laravel Purity is an elegant and efficient filtering and sorting package for Laravel that simplifies this task for you. By simply adding filter() to your Eloquent query, you can add the ability for frontend users to apply filters and sort data according to their preferences.

Laravel Purity supports various filter methods out of the box. i provided you a list of avalable filters below. You can also filter by relation columns using dot notation such as user.name or product.category.name. Moreover, you can define custom filters using closures if you need more flexibility.

Laravel Purity also allows you to sort data by one or more columns using asc or desc order. You can also sort by relation columns.

To use Laravel Purity in your project, you need to install it via composer:

```bash
composer require abbasudo/laravel-purity
```

Then you need to use the Filterable and Sortable trait in your model:

```php
use AbbasUdo\LaravelPurity\Traits\Filterable;
use AbbasUdo\LaravelPurity\Traits\Sortable;

class Product extends Model
{
    use Filterable;
    use Sortable;

}
```

Now you can use `filter()` and `sort()` method on your query builder:
```php
$products = Product::filter()->sort()->get();
```
This will apply the filters and sorting based on the request parameters. For example,
`/products?price[gte]=100&price[lte]=500&category[in][]=electronics&category[in][]=books&sort=rating:desc,name`
This will return the products that have a price between 100 and 500 dollars and belong to either electronics or books category sorted by rating in descending order and name in ascending order.

available filters are:

| Operator        | Description                              |
|-----------------|------------------------------------------|
| `$eq`           | Equal                                    |
| `$eqc`          | Equal (case-sensitive)                   |
| `$ne`           | Not equal                                |
| `$lt`           | Less than                                |
| `$lte`          | Less than or equal to                    |
| `$gt`           | Greater than                             |
| `$gte`          | Greater than or equal to                 |
| `$in`           | Included in an array                     |
| `$notIn`        | Not included in an array                 |
| `$contains`     | Contains                                 |
| `$notContains`  | Does not contain                         |
| `$containsc`    | Contains (case-sensitive)                |
| `$notContainsc` | Does not contain (case-sensitive)        |
| `$null`         | Is null                                  |
| `$notNull`      | Is not null                              |
| `$between`      | Is between                               |
| `$startsWith`   | Starts with                              |
| `$startsWithc`  | Starts with (case-sensitive)             |
| `$endsWith`     | Ends with                                |
| `$endsWithc`    | Ends with (case-sensitive)               |
| `$or`           | Joins the filters in an "or" expression  |
| `$and`          | Joins the filters in an "and" expression |


As you can see, Laravel Purity makes it easy and elegant to filter and sort data in Laravel without writing complex queries or logic. It also follows the best practices of naming conventions and validation rules for filtering and sorting parameters.

If you want to learn more about Laravel Purity features such as custom filters, restrict filters, silent execution, etc, please visit its [documentation](https://abbasudo.github.io/laravel-purity/?utm_source=dev.to&utm_medium=Correct+way).

I hope you enjoyed this post. If you have any questions or feedback please feel free to leave a comment below.