Laravel PurityLaravel Purity
Introduction
  • Installation
  • Basic Usage
  • Additional Tutorials
  • Available Filters
  • Filtering
  • Sorting
  • Rename Fields
  • Relation Fields
  • Params Source
  • Allowed Fields
  • Livewire
  • Silent Exceptions
  • Filter

    • Restrict
    • Custom Filters
  • Sort

    • Null Sort
  • Upgrade Guide
Client Package
Ask a Question
GitHub
Introduction
  • Installation
  • Basic Usage
  • Additional Tutorials
  • Available Filters
  • Filtering
  • Sorting
  • Rename Fields
  • Relation Fields
  • Params Source
  • Allowed Fields
  • Livewire
  • Silent Exceptions
  • Filter

    • Restrict
    • Custom Filters
  • Sort

    • Null Sort
  • Upgrade Guide
Client Package
Ask a Question
GitHub
  • Allowed Fields

    • Allowed Fields

Allowed Fields

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

// 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:

// 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.

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

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

TIP

filterFields and sortFields will overwrite fields defined in the model.

Edit this page
Last Updated:
Contributors: Abbas mkhzomi
Prev
Relation Fields
Next
Livewire