在块函数Laravel 4上使用变量

时间:2014-08-07 21:45:28

标签: php laravel orm laravel-4

我需要在laravel 4的方法中使用变量,如何将这个变量传递给eloquent的方法块?

$variableonmethod=array('value1','value2');
PreAlumno::chunk(200, function($prealumnos) {
        foreach ($prealumnos as $pre) {
           //do something with variableonmethod
        }
});

2 个答案:

答案 0 :(得分:8)

如果您只想阅读信息

X

但是,如果你想阅读数组并更改它们的价值(检查>>> [i[2] for i in sorted(zip(Z, Y, X), key=lambda x: (x[0], x[1]))] ['d', 'h', 'b', 'i', 'f', 'a', 'c', 'e', 'g'] 上的&

$variableonmethod=array('value1','value2');
PreAlumno::chunk(200, function($prealumnos) use ($variableonmethod) {
    echo variableonmethod[0]; // prints `value1`
    variableonmethod[0] = 'Hola';
    echo variableonmethod[0]; // prints `Hola`
    foreach ($prealumnos as $pre) {
       //do something with variableonmethod
    }
});
echo variableonmethod[0]; // prints `value1`

答案 1 :(得分:5)

您可以使用use($ var)传递数组来实现结果。

$variableonmethod=array('value1','value2');
PreAlumno::chunk(200, function($prealumnos) use ($variableonmethod) {
    foreach ($prealumnos as $pre) {
       //do something with variableonmethod
    }
});