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
    • 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

Lifecycle Hooks

The Filterable base class provides two lifecycle hooks that let you modify the query builder before and after filters are applied.

These hooks are optional and can be defined directly inside your filter class.


initially()

Runs before any filters are executed. It’s perfect for setting up default query conditions or preparing the builder.

use Kettasoft\Filterable\Filterable;
use Illuminate\Database\Eloquent\Builder;

class ProductFilter extends Filterable
{
    protected function initially(Builder $builder): void
    {
        // Example: Apply a global condition before filtering
        $builder->where('is_active', true);
    }
}

finally()

Runs after all filters have been processed. It allows you to finalize or clean up your query logic.

protected function finally(Builder $builder): void
{
    // Example: Apply a default sort order
    if (! $builder->getQuery()->orders) {
        $builder->orderBy('created_at', 'desc');
    }
}

βš™οΈ How It Works

  • initially() is invoked right before any filter method runs.
  • finally() is called after all filter methods have finished.
  • Both are optional β€” if not defined, they’re skipped automatically.
  • They share the same $builder instance used by the engine, so any change persists through the filtering process.

πŸͺ„ CLI Integration

When generating a new filter using the Artisan command:

php artisan make:filter ProductFilter

Both initially() and finally() methods are automatically added to the stub file, ready for customization.


πŸ’‘ Practical Use Cases

  • Use initially() to:

    • Apply global constraints (is_active = true, tenant_id = currentTenant()).
    • Add necessary joins or eager loads before filters run.
  • Use finally() to:

    • Apply ordering or limits.
    • Clean up relations or add post-filter transformations.
Edit this page
Last Updated:
Contributors: kettasoft
Next
Header-Driven Filter Mode