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

Allowed Fields

By default, Framework\Crud 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();

filterFields and sortFields will overwrite fields defined in the model.

PreviousChanging Params SourceNextQueries and javascript examples

Last updated 4 months ago

📜