Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Purity Laravel Package

abbasudo/laravel-purity

Laravel Purity adds elegant filtering and sorting to Eloquent queries via a simple filter() method. Let frontend users drive complex query conditions using URL query string parameters (e.g., filters[title][$contains]=Purity), with minimal boilerplate.

View on GitHub
Deep Wiki
Context7

title: Restrict prev: text: 'Silent Exceptions' link: '/advanced/silent' next: text: 'Custom Filters' link: '/advanced/filter/custom'

Global Filters

purity validates allowed filters in the following order of priority:

  • Filters specified in the filters configuration in the configs/purity.php file.
// configs/purity.php
'filters' => [
  EqualFilter::class,
  InFilter::class,
],
  • Filters declared in the $filters variable in the model.

note that $filters will overwrite config filters.

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

note that, filterBy will overwrite all other defined filters.

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
$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
$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)
Post::restrictedFilters(['title' => ['$eq']])->filter()->get();

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

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

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport