extract database database record by blocks

0

I have a table in a BD and I want to extract from it 10-in-10 records to store an image and have no memory problems. I'm working with laravel

The code I use is:

$imagenes = new Collection();
DB::table('inscripciones')->orderBy('id')->chunk(10, function ($inscripciones) 
{
   foreach ($inscripciones as $inscripcion)
        {
            $imagenes->push($inscripcion->foto);
            $imagenes->push($inscripcion->scan_pasaporte);
        };
 });

But it gives an error: It tells me that $imagenes is not defined. It can be solved by entering the definition ($imagenes = new Collection();) within the foreach, but of course, the result is not what you want.

Any way that $imagenes is valid within the function?

I feel if I do not explain it very well. Thanks

    
asked by Gema 22.08.2018 в 17:58
source

1 answer

0

Try passing the variable $imagenes to the anonymous function using use ( in your case it is necessary to use & to pass it by reference )

Something like this:

$imagenes = new Collection();
DB::table('inscripciones')->orderBy('id')->chunk(10, function ($inscripciones) use (&$imagenes) 
{
...

I leave the link to the official documentation: link

    
answered by 22.08.2018 в 18:47