Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to access variable outside laravel collection->each

I am trying to access variable outside laravel collection->each, but I get ..

Undefined variable: headers

Here's my code:

public function bulkCoding(Request $request) {

    Excel::load($request->file, function($reader) {
        $headers  = $reader->get()->first()->keys();

        // Loop through all sheets
        $reader->each(function($sheet) {
            $headers->each(function ($title) {
                dd($title);
            });
        });
    });

}
like image 508
CairoCoder Avatar asked Dec 23 '22 10:12

CairoCoder


1 Answers

You need to use the use() to pass variable inside the closure scope:

each(function($sheet) use($headers) {
like image 147
Alexey Mezenin Avatar answered Dec 28 '22 06:12

Alexey Mezenin