begineeeerrrr
begineeeerrrr

Reputation: 343

Laravel excel, why function($reader) cannot get variables?

Currently im using Laravel-Excel(MaatWebsite). I have codes like this in controller

public function house($id)
{
  $furniture_number = Furniture::where('id', $id)->first();

  $rows = Excel::load('storage\\app\\public\\upload\\furniture.xlsx', function($reader)
  {
    $reader->noHeading(); 
    $reader->takeColumns($furniture_number->amount);
  })->get();
}

The dd($furniture_number->amount) return "10"

Undefined variable: furniture_number (at the $reader->takeColumns($furniture_number->amount); line)

Upvotes: 0

Views: 317

Answers (1)

Jed
Jed

Reputation: 216

You need to pass the $furniture_number variable to the closure with the 'use' statement:

  $rows = Excel::load('storage\\app\\public\\upload\\furniture.xlsx', function($reader) use ($furniture_number) {
    $reader->noHeading(); 
    $reader->takeColumns($furniture_number->amount);
  })->get();

Upvotes: 1

Related Questions