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
Post a Comment