arrays - PHP: Pass variable to function -


looks simple (and maybe) need pass received variable in function function. here's code:

pd: using laravel eloquent's scopes

class myparentmodel extends model {      public function scopemyscope($query, $var_i_want_to_pass=[]) {          return $query->with('model'])->wherehas('model', function($q, $var_i_want_to_pass=[]) {              $q->where('colum1',$var_i_want_to_pass[0])->where('colum2',$var_i_want_to_pass[1])->where('colum3',$var_i_want_to_pass[2]);          })->take(10);      } } 

and want this:

$result = myparentmodel::myscope([3,1,6])->get(); 

i resolve using use:

class myparentmodel extends model {      public function scopemyscope($query, $var_i_want_to_pass) {          return $query->with('model'])->wherehas('model', function($q) use ($var_i_want_to_pass) {              $q->where('colum1',$var_i_want_to_pass[0])->where('colum2',$var_i_want_to_pass[1])->where('colum3',$var_i_want_to_pass[2]);          })->take(10);      } } 

Comments

Popular posts from this blog

xml - Extract substrings with XSLT -

How can do a tuple comparison in coffeescript similar to that in python? -

math - "ELO Rating" and how does "TSR Rating" work? -