> For the complete documentation index, see [llms.txt](https://pixiedia.gitbook.io/maginium/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pixiedia.gitbook.io/maginium/the-basics/crud/filters-and-sorts/restrict.md).

# Restrict

#### Global Filters

purity validates allowed filters in the following order of priority:

* Filters specified in the `filters` configuration in the `configs/purity.php` file.

```php
// configs/purity.php
'filters' => [
  EqualFilter::class,
  InFilter::class,
],

```

* Filters declared in the `$filters` variable in the model.

{% hint style="info" %}
Note that $filters will overwrite config filters.
{% endhint %}

```php
// App\Models\Post

private array $filters = [
  '$eq',
  '$in',
];

// or

private array $filters = [
  EqualFilter::class,
  InFilter::class,
];
```

* Filters passed as an array to the `filterBy()` function.

{% hint style="info" %}
Note that, \`filterBy\` will overwrite all other defined filters.
{% endhint %}

```php
Post::filterBy('$eq', '$in')->filter()->get();
// or
Post::filterBy(EqualFilter::class, InFilter::class)->filter()->get();
```

#### Restrict by Field

There are three available Methods for your convenience. They take priority respectively.

* **Method 1: Define restricted filters inside `$filterFields` property, as shown below**

```php
$filterFields = [
  'title' => ['$eq'],  // title will be limited to the eq operator
  'title' => '$eq',    // works only for one restricted operator
  'title:$eq',         // same as above
  'title',             // this won't be restricted to any operator
];
```

The drawback here is that you have to define all the allowed fields, regardless of any restriction fields.

* **Method 2: Define them inside `$restrictedFilters` property**

```php
$restrictedFields = [
  'title' => ['$eq', '$eq'],  // title will be limited to the eq operator
  'title:$eq,$in'             // same as above
  'title'                     // this won't be restricted to any operator
];
```

* **Method 3: Finally, you can set it on the Eloquent builder, which takes the highest priority (overwrite all the above options)**

```php
Post::restrictedFilters(['title' => ['$eq']])->filter()->get();
```

{% hint style="warning" %}
All field-restricted filter operations are respected to filters defined in $filter in the model. This means you are not allowed to restrict a field operation not permitted in restricted fields.
{% endhint %}

```php
$filters = ['$eq'];
$restrictedFilters = ['title' => ['$eqc']] // This won't work

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pixiedia.gitbook.io/maginium/the-basics/crud/filters-and-sorts/restrict.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
