{"record":{"id":"ba0d52e4478077fe","repo":"twigphp/Twig","slug":"the-filter-filter-expects-a-sequence-mapping-or-traversable","errorCode":null,"errorMessage":"The \"filter\" filter expects a sequence/mapping or \"Traversable\", got \"%s\".","messagePattern":"The \"filter\" filter expects a sequence/mapping or \"Traversable\", got \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":2058,"sourceCode":"                    if (null !== $index) {\n                        $policy->checkPropertyAllowed($item, (string) $index);\n                    }\n                }\n            }\n        }\n\n        return array_column($array, $name, $index);\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */\n    public static function filter(Environment $env, bool $isSandboxed, $array, $arrow)\n    {\n        if (!is_iterable($array)) {\n            throw new RuntimeError(\\sprintf('The \"filter\" filter expects a sequence/mapping or \"Traversable\", got \"%s\".', get_debug_type($array)));\n        }\n\n        self::checkArrow($isSandboxed, $arrow, 'filter', 'filter');\n\n        if (\\is_array($array)) {\n            return array_filter($array, $arrow, \\ARRAY_FILTER_USE_BOTH);\n        }\n\n        // the IteratorIterator wrapping is needed as some internal PHP classes are \\Traversable but do not implement \\Iterator\n        return new \\CallbackFilterIterator(new \\IteratorIterator($array), $arrow);\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */\n    public static function find(Environment $env, bool $isSandboxed, $array, $arrow)","sourceCodeStart":2040,"sourceCodeEnd":2076,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L2040-L2076","documentation":"The Twig 'filter' filter (CoreExtension::filter) applies an arrow function to a sequence/mapping or Traversable. It throws this RuntimeError when the input is not iterable, reporting the actual debug type of the value.","triggerScenarios":"Using {{ value|filter(a => a.active) }} where value is null, a scalar, or a non-Traversable object — e.g. a null result from a repository or missing context variable.","commonSituations":"Passing a single entity instead of a collection; null from a failed lookup or unset context key; custom data objects that are not IteratorAggregate; strict_variables off hiding the null until the filter runs.","solutions":["Default the value to an array: {{ (items ?? [])|filter(a => a.active) }}","Ensure the variable in the context is an array or implements Traversable (e.g. Doctrine Collection does)","Guard with {% if items is iterable %} or the 'iterable' test before filtering"],"exampleFix":"// before\n{{ items|filter(i => i.enabled) }}\n\n// after\n{{ (items ?? [])|filter(i => i.enabled) }}","handlingStrategy":"validation","validationCode":"if (!is_iterable($items)) { $items = []; }\n// or in template: {% if items is iterable %}{{ items|filter(i => i.active) }}{% endif %}","typeGuard":"function isFilterable($v): bool { return is_array($v) || $v instanceof \\Traversable; }","tryCatchPattern":"try { $out = $twig->render($tpl, $ctx); } catch (\\Twig\\Error\\RuntimeError $e) { if (str_contains($e->getMessage(), '\"filter\" filter')) { /* coerce input to array/Traversable */ } }","preventionTips":["Coerce optional context values with ?? [] before collection filters","Return Doctrine Collections or arrays from data providers","Guard with the iterable test in templates"],"tags":["twig","filter","arrow-function","iterable"],"backgroundTag":"incompatible-source-type","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}