PHP:将变量传递给函数

时间:2015-09-21 05:38:52

标签: php arrays function laravel-5 parameter-passing

看起来简单(也许)但我需要将函数中的接收变量传递给另一个函数。这是我的代码:

PD:我使用的是Laravel Eloquent的范围

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);

    }
}

我想这样做:

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

1 个答案:

答案 0 :(得分:3)

我使用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);

    }
}