# Allowed Fields

By default, Framework\Crud allows every database column and all model relations (that have a defined return type) to be filtered.

```php
// App\Models\User

public function posts(): Illuminate\Database\Eloquent\Relations\HasMany // This is mandatory
{
    return $this->hasMany(Post::class);
}
```

you can overwrite the allowed columns as follows:

```php
// App\Models\User

protected $filterFields = [
  'email',
  'mobile',
  'posts', // relation
];

protected $sortFields = [
  'name',
  'mobile',
];
```

any field other than email, mobile, or posts will be rejected when filtering.

## Overwrite Allowed Fields

to overwrite allowed fields in the controller add `filterFields` or `sortFields` before calling filter or sort method.

```php
Post::filterFields('title', 'created_at')->filter()->get();

Post::sortFields('created_at', 'updated_at')->sort()->get();
```

{% hint style="info" %}
filterFields and sortFields will overwrite fields defined in the model.
{% endhint %}


---

# Agent Instructions: 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/allowed-fields.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.
