Filterable
Home
๐Ÿ“ฆ Installation
GitHub
Home
๐Ÿ“ฆ Installation
GitHub
  • Home
  • Introduction
  • Installation
  • How It Works
  • Engines

    • Invokable
    • Tree
    • Ruleset
    • Expression
  • Features

    • Header-Driven Filter Mode
    • Auto Register Filterable Macro
    • Conditional Logic
    • Filter Aliases
    • Through callbacks
    • Auto Binding
  • Execution

    • Invoker
  • API

    • Payload
    • Sorter
  • Profiler
  • Sorting
  • Authorization
  • Validation
  • Sanitization

Apply Custom Query Callbacks using through()

Overview

The through() method allows you to apply an array of custom query callbacks to the Eloquent builder instance within the Filterable class.

This gives you a powerful way to manipulate queries using closures (similar to pipelines), before or after applying filters โ€” enabling advanced use cases such as chaining global conditions, joins, or even reordering logic externally.

๐Ÿงช Usage

use Kettasoft\Filterable\Filterable;
use App\Models\Post;

$filter = Filterable::create()->setBuilder(Post::query());

$results = $filter->through([
    fn ($builder) => $builder->where('status', 'published'),
    fn ($builder) => $builder->orderByDesc('created_at'),
]);

$posts = $results->apply()->get();

You can also chain with other Filterable methods:

$results = Filterable::create()
    ->setBuilder(Post::query())
    ->through([
        fn ($builder) => $builder->where('is_active', true),
    ])
    ->apply();

โš ๏ธ Notes

  • Every item in the array passed to through() must be a valid callable.
  • If a non-callable value is passed, an InvalidArgumentException will be thrown.
  • Callbacks receive the query builder as the only argument and must return the modified builder.
  • This method is chainable and returns the Filterable instance.

๐Ÿ’ก Benefits

  • ๐Ÿ”„ Adds a flexible, composable way to extend queries externally.
  • ๐Ÿงช Great for injecting reusable query logic without modifying filters.
  • ๐Ÿšซ Prevents tight coupling between filter logic and query logic.
  • โœ… Compatible with both eager and late filter application (apply()).
  • ๐Ÿงฑ Clean separation of filtering rules and additional query logic.
Edit this page
Last Updated:
Contributors: Abdalrhman Emad Saad
Prev
Filter Aliases
Next
Auto Binding