Changing Laravel Variable in Collection Filters

Is there a way to do $var = 7 in filtering a collection as follows?

 $var = 1; $collection->filter(function( $q ) use ($var){ if( true ){ $var = 7; return true; } }); dd( $var ); 

This is currently not working and I am still getting 1

I also tried using global , but still I get 1

+5
source share
1 answer

Pass the variable by reference , then it will be changed, for example.

  $ collection-> filter (function ($ q) use ( & $ var) {
                                       // ^ See here
+7
source

All Articles