Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel change variable within collection filtering

Is there anyway to make $var = 7 in collection filtering like this?

$var = 1;

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

dd( $var );

Currently this doesn't work and what I get is still 1

I've also tried using global but still I get 1

like image 431
AliRNazari Avatar asked Aug 30 '15 12:08

AliRNazari


1 Answers

Pass the variable by reference, then it will get changed, e.g.

$collection->filter(function( $q ) use (&$var){
                                      //^ See here
like image 96
Rizier123 Avatar answered Sep 18 '22 15:09

Rizier123