FilterableFilterable
Home
📦 Installation
  • Setting Up Filterable
  • Discover Command
  • Listing All Filters
  • Testing Filters
  • Inspecting Filterable Classes
  • Caching
GitHub
Home
📦 Installation
  • Setting Up Filterable
  • Discover Command
  • Listing All Filters
  • Testing Filters
  • Inspecting Filterable Classes
  • Caching
GitHub
  • Home
  • Introduction
  • Installation
  • Service Provider
  • How It Works
  • Engines

    • Invokable

      • Overview
      • Annotations

        • Overview
        • Authorize
        • SkipIf
        • Trim
        • Sanitize
        • Cast
        • DefaultValue
        • MapValue
        • Explode
        • Required
        • In
        • Between
        • Regex
        • Scope
    • Tree
    • Ruleset
    • Expression
  • Features

    • Lifecycle Hooks
    • Header-Driven Filter Mode
    • Auto Register Filterable Macro
    • Conditional Logic
    • Filter Aliases
    • Through callbacks
    • Auto Binding
    • Custom engines
    • Data Provisioning
  • Execution

    • Invoker
  • API Reference

    • Filterable
    • Filterable facade
    • Payload
    • Sorter
  • Caching

    • Overview
    • Getting Started
    • Strategies
    • Auto Invalidation
    • Cache Profiles
    • Scoping Cache
    • Monitoring Cached Items
    • API Reference
    • Examples
  • CLI

    • Setup Filterable
    • Discover Filters
    • Test Filter
    • List Filters
    • Inspect Filter
  • Exceptions
  • Event System
  • Profile Management
  • Profiler
  • Sorting
  • Authorization
  • Validation
  • Sanitization

#[Cast]

Stage: TRANSFORM (2)

Casts the payload value to a specific type using the Payload's as* methods.


Parameters

ParameterTypeRequiredDescription
$typestring✅The target type name (maps to Payload::as{Type}())

Supported Types

TypeMaps ToDescription
int$payload->asInt()Cast to integer
boolean$payload->asBoolean()Cast to boolean
array$payload->asArray()Decode JSON or return existing array
carbon$payload->asCarbon()Parse to Carbon date instance
slug$payload->asSlug()Convert to URL-friendly slug
like$payload->asLike()Wrap with % for LIKE queries

Usage

Cast to Integer

use Kettasoft\Filterable\Engines\Foundation\Attributes\Annotations\Cast;

#[Cast('int')]
protected function views(Payload $payload)
{
    // "42" → 42
    return $this->builder->where('views', '>=', $payload->value);
}

Cast to Boolean

#[Cast('boolean')]
protected function isFeatured(Payload $payload)
{
    // "true" → true, "false" → false
    return $this->builder->where('is_featured', $payload->value);
}

Cast to Array (from JSON)

#[Cast('array')]
protected function tags(Payload $payload)
{
    // '["php","laravel"]' → ['php', 'laravel']
    $tags = $payload->value;
    return $this->builder->whereIn('tag', $tags);
}

Behavior

ScenarioResult
Cast type is supportedValue is cast and returned
Cast type is not supportedStrictnessException is thrown
Cast fails (invalid value)StrictnessException is thrown
Edit this page
Last Updated:
Contributors: kettasoft
Prev
Sanitize
Next
DefaultValue