试图访问可变的外部laravel集合 - >每个

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

}

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

each(function($sheet) use($headers) {