8

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

}
CairoCoder
  • 3,091
  • 11
  • 46
  • 68

1 Answers1

15

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

each(function($sheet) use($headers) {
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279