Maginium
Docs
Docs
  • Welcome
  • Getting Started
    • Quickstart
    • Installation
    • Structure
  • The Basics
    • Concurrency
    • 🔤Strings
    • ⚙️Actions
    • 🗃️Cache
    • 🗂️Collections
    • 🔲Component
    • ⚙️Config
    • 💬Console
    • 🗃️Database
      • Overview
      • Eloquent
      • Seeder
      • Migration
      • Models
      • Factories
      • Schemas
      • Traits
      • Concerns
    • 🛅Container
    • 🔠Enum
    • 📦Event
    • 📝Log
    • 🔢Pagination
    • 💬Prompts
    • 🌐Request
    • 📡Response
    • ⚡Redis
    • 📏Resize
    • 🔌Serializer
      • Json
      • Serializer
      • Closure Serializer
    • 🔑Uuid
    • 👤Avatar
    • 🔐Hashing
    • 📤Dto
      • Overview
      • Attributes
    • 🌍Locale
    • 🔗Token
      • Api Keys
      • Admin Token
      • Customer Token
    • 🕘Message Queue
    • 🖼️Media
    • Helpers
    • 📜Crud
      • Filters and Sorts
        • introduction
        • Basic Usage
        • JS Examples
          • Available Methods
          • Sorts
          • Filters
        • Rename Fields
        • Relation Fields
        • Changing Params Source
        • Allowed Fields
        • Queries and javascript examples
        • Sort null values last
        • Restrict
        • Custom Filters
  • Commerce Modules
    • API Key Module
    • Auth Module
    • Cart Module
    • Customer Module
    • Inventory Module
    • Fulfillment Module
    • Order Module
    • Payment Module
    • Pricing Module
    • Product Module
    • Promotion Module
    • User Module
  • SDKs and Tools
    • create-maginium-app
    • Maginium CLI
    • JS SDK
    • Storefront Starter
Powered by GitBook
On this page
  1. The Basics
  2. Crud
  3. Filters and Sorts

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.

// 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();

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
PreviousSort null values lastNextCustom Filters

Last updated 4 months ago

📜