I need to use a variable into a method on laravel 4, how can i pass this variable to the method chunk from eloquent?
$variableonmethod=array('value1','value2');
PreAlumno::chunk(200, function($prealumnos) {
foreach ($prealumnos as $pre) {
//do something with variableonmethod
}
});
If you like only read information
$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`
But, if you like to read array AND CHANGE THEIR VALUES (check & on use
)
$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 `Hola`
You can pass the array using the use ($var) to achieve your result.
$variableonmethod=array('value1','value2');
PreAlumno::chunk(200, function($prealumnos) use ($variableonmethod) {
foreach ($prealumnos as $pre) {
//do something with variableonmethod
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With