> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eloquentfiltering.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Eloquent Filtering

<Frame>
  <img src="https://mintcdn.com/indexzer0/6juTwvp1UUZYBDoo/images/hero.png?fit=max&auto=format&n=6juTwvp1UUZYBDoo&q=85&s=cba80a4eed07f0550f36c959fea99e13" noZoom width="1536" height="768" data-path="images/hero.png" />
</Frame>

<br />

<div
  style={{
    display: "flex"
}}
  class="justify-center"
>
  <a
    style={{
        borderStyle: "none",
        marginTop: "0.125rem",
        marginBottom: "0.125rem",
    }}
    target="_blank"
    href="https://packagist.org/packages/indexzer0/eloquent-filtering"
  >
    <img
      style={{
            margin: 0,
            cursor: "pointer",
        }}
      noZoom
      src="https://img.shields.io/packagist/v/IndexZer0/eloquent-filtering?style=for-the-badge"
      alt="Latest Version on Packagist"
    />
  </a>

  <a
    style={{
        borderStyle: "none",
        marginTop: "0.125rem",
        marginBottom: "0.125rem",
        marginLeft: "0.25rem",
    }}
    target="_blank"
    href="https://packagist.org/packages/indexzer0/eloquent-filtering"
  >
    <img
      style={{
            margin: 0,
            cursor: "pointer",
        }}
      noZoom
      src="https://img.shields.io/packagist/dt/indexzer0/eloquent-filtering.svg?style=for-the-badge&color=007ec6"
      alt="Total Downloads"
    />
  </a>

  <a
    style={{
        borderStyle: "none",
        marginTop: "0.125rem",
        marginBottom: "0.125rem",
        marginLeft: "0.25rem",
    }}
    target="_blank"
    href="https://github.com/indexzer0/eloquent-filtering"
  >
    <img
      style={{
            margin: 0,
            cursor: "pointer",
        }}
      noZoom
      src="https://img.shields.io/github/stars/indexzer0/eloquent-filtering?logo=github&style=for-the-badge"
      alt="GitHub Stars"
    />
  </a>
</div>

<div
  style={{
    display: "flex"
}}
  class="justify-center"
>
  <a
    style={{
        borderStyle: "none",
        marginTop: "0.125rem",
        marginBottom: "0.125rem",
        marginLeft: "0.25rem",
        cursor: "default",
    }}
    target="_blank"
    href="https://packagist.org/packages/indexzer0/eloquent-filtering"
  >
    <img
      style={{
            margin: 0,
            cursor: "pointer",
        }}
      noZoom
      src="http://poser.pugx.org/indexzer0/eloquent-filtering/require/php?style=for-the-badge"
      alt="PHP Version Require"
    />
  </a>

  <a
    style={{
        borderStyle: "none",
        marginTop: "0.125rem",
        marginBottom: "0.125rem",
        marginLeft: "0.25rem",
    }}
    target="_blank"
    href="https://packagist.org/packages/indexzer0/eloquent-filtering"
  >
    <img
      style={{
            margin: 0,
            cursor: "pointer",
        }}
      noZoom
      src="http://poser.pugx.org/indexzer0/eloquent-filtering/require/illuminate/contracts?style=for-the-badge"
      alt="Laravel Version Require"
    />
  </a>
</div>

<div
  style={{
    display: "flex"
}}
  class="justify-center"
>
  <a
    style={{
        borderStyle: "none",
        marginTop: "0.125rem",
        marginBottom: "0.125rem",
        marginLeft: "0.25rem",
        cursor: "default",
    }}
    target="_blank"
    href="https://github.com/indexzer0/eloquent-filtering/actions?query=workflow%3Arun-tests+branch%3Amain"
  >
    <img
      style={{
            margin: 0,
            cursor: "pointer",
        }}
      noZoom
      src="https://img.shields.io/github/actions/workflow/status/indexzer0/eloquent-filtering/run-tests.yml?branch=main&label=tests&style=for-the-badge"
      alt="GitHub Tests Action Status"
    />
  </a>

  <a
    style={{
        borderStyle: "none",
        marginTop: "0.125rem",
        marginBottom: "0.125rem",
        marginLeft: "0.25rem",
    }}
    target="_blank"
    href="https://codecov.io/gh/IndexZer0/eloquent-filtering"
  >
    <img
      style={{
            margin: 0,
            cursor: "pointer",
        }}
      noZoom
      src="https://img.shields.io/codecov/c/github/IndexZer0/eloquent-filtering?token=34B3NIPBRM&style=for-the-badge&logo=codecov"
      alt="Codecov"
    />
  </a>
</div>

## Overview

*Eloquent Filtering* **simplifies** implementing search functionality for your Eloquent models, whether simple or complex, by **eliminating** the need for custom query logic.
It allows you to **easily** define and manage filters directly within your models, and **seamlessly** apply them using incoming HTTP request data to dynamically filter your models.

With this package, you can build more readable, maintainable, and scalable code, **boosting** your productivity and **speeding up** development.

Whether you’re building APIs, dashboards, or advanced search systems, *Eloquent Filtering* provides a **powerful** and **flexible** way to streamline your Eloquent queries, making it easier to manage and extend your application’s filtering capabilities.

## Quick Look

```php theme={null}
class Product extends Model implements IsFilterable
{
    use Filterable;

    public function allowedFilters(): AllowedFilterList
    {
        return Filter::only(
            Filter::field('name', [FilterType::EQUAL]),
        );
    }
}

$products = Product::filter([
    [
        'target' => 'name',
        'type'   => '$eq',
        'value'  => 'TV'
    ]
])->get();
```

## Features

<CardGroup cols={2}>
  <Card title="Large Filter Library" icon="list" href="/v2/available-filters/full-filter-list#field-filters" horizontal>
    Many core filter methods.
  </Card>

  <Card title="Field Filters" icon="database" href="/v2/basics/in-detail#field" horizontal>
    Filter your models fields.
  </Card>

  <Card title="Relationship Filters" icon="link" href="/v2/basics/in-detail#relation" horizontal>
    Filter by relationship existence.
  </Card>

  <Card title="Pivot Filters" icon="scale-unbalanced" href="/v2/digging-deeper/pivot-filters#overview" horizontal>
    Filter your intermediate table.
  </Card>

  <Card title="Required Filters" icon="asterisk" href="/v2/digging-deeper/required-filters#overview" horizontal>
    Set filters as required.
  </Card>

  <Card title="Validation" icon="shield-check" href="/v2/digging-deeper/validation-rules#overview" horizontal>
    Add validation to your defined filters.
  </Card>

  <Card title="Custom Filters" icon="code" href="/v2/digging-deeper/custom-filters#overview" horizontal>
    Create your own custom filters.
  </Card>

  <Card title="Alias" icon="eye" href="/v2/digging-deeper/aliasing-targets#overview" horizontal>
    Alias fields and relationships.
  </Card>

  <Card title="Filter JSON Columns" icon="brackets-curly" href="/v2/digging-deeper/json-path-wildcards#overview" horizontal>
    Json path wildcard support.
  </Card>

  <Card title="Granular Control" icon="filter-list" href="/v2/digging-deeper/allowed-types#overview" horizontal>
    Specify filter types.
  </Card>
</CardGroup>

## Field And Relationship Filter Example

```php theme={null}
class Product extends Model implements IsFilterable
{
    use Filterable;

    public function allowedFilters(): AllowedFilterList
    {
        return Filter::only(
            Filter::field('name', [FilterType::EQUAL]),
            Filter::relation('manufacturer', [FilterType::HAS])->includeRelationFields()
        );
    }

    public function manufacturer(): HasOne
    {
        return $this->hasOne(Manufacturer::class);
    }
}

class Manufacturer extends Model implements IsFilterable
{
    use Filterable;

    public function allowedFilters(): AllowedFilterList
    {
        return Filter::only(
            Filter::field('name', [FilterType::EQUAL])
        );
    }
}

$filters = [
    [
        'target' => 'name',
        'type'   => '$eq',
        'value'  => 'TV',
    ],
    [
        'type'   => '$has',
        'target' => 'manufacturer',
        'value'  => [
            [
                'type'   => '$eq',
                'target' => 'name',
                'value'  => 'Sony',
            ]
        ]
    ]
];

$sql = Product::filter($filters)->toRawSql();
```

```sql theme={null}
SELECT *
FROM "products"
WHERE "products"."name" = 'TV'
  AND EXISTS (
    SELECT *
    FROM "manufacturers"
    WHERE "products"."manufacturer_id" = "manufacturers"."id"
      AND "manufacturers"."name" = 'Sony'
  )
```
