繁体   English   中英

PHP:将变量传递给函数

[英]PHP: Pass variable to function

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

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

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

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM