douce1368
douce1368
2017-08-03 02:25
浏览 93
已采纳

Laravel excel,为什么函数($ reader)无法获取变量?

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)

图片转代码服务由CSDN问答提供 功能建议

目前正在使用Laravel-Excel(MaatWebsite)。 我在控制器中有这样的代码

 公共功能室($ id)
 {
 $ furniture_number = Furniture :: where('id',$ id) - &gt  ; first(); 
 
 $ rows = Excel :: load('storage \\ app \\ public \\ upload \\ furniture.xlsx',function($ reader)
 {
 $ reader->  noHeading(); 
 $ reader-> takeColumns($ furniture_number-> amount); 
}) - > get(); 
} 
   
 
 <  p> dd($ furniture_number-&gt;金额)返回“10” 
 
 

未定义变量:furniture_number(在$ reader-&gt; takeColumns($ furniture_number-&gt;金额);行 )

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyange1101
    dongyange1101 2017-08-03 02:30
    已采纳

    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();
    
    点赞 评论

相关推荐