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

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -