Answers.Body: <p>Use the <code>filter</code> filter.</p>
  <pre class="lang-twig prettyprint-override"><code>{% set filtered = items|filter(item =&gt; item &gt; 2) %}
  </code></pre>
  <p><a href="https://craftcms.com/docs/3.x/dev/filters.html#filter" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/dev/filters.html#filter</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-22T12:04:04.797Z
Answers.LastActivityDate: 2022-05-22T12:04:04.797Z
Answers.UserDisplayName: James Smith
Body: <p>How to implement Laravel's collection <code>filter</code> method inside twig?</p>
  <p>Laravel:</p>
  <pre><code>$collection = collect([1, 2, 3, 4]);
   
  $filtered = $collection-&gt;filter(function ($value, $key) {
      return $value &gt; 2;
  });
  $filtered-&gt;all();
  </code></pre>
  <p>How can I do this inside twig?</p>
  
Tags: <templating><collections>
Title: How to implement Laravel's collection `filter` method inside twig?
ThreadText: # How to implement Laravel's collection `filter` method inside twig?
  
  (Asked by: username_0 on 2022-05-22)
  
  How to implement Laravel's collection `filter` method inside twig?
  
  Laravel:
  
  ```
  $collection = collect([1, 2, 3, 4]);
   
  $filtered = $collection->filter(function ($value, $key) {
      return $value > 2;
  });
  $filtered->all();
  
  ```
  
  How can I do this inside twig?
  
  **Tags:** templating, collections
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-05-22. Score: 2 | Accepted answer
  
  Use the `filter` filter.
  
  ```twig
  {% set filtered = items|filter(item => item > 2) %}
  
  ```
  
  <https://craftcms.com/docs/3.x/dev/filters.html#filter>
  
  
  ---
  
  