简体   繁体   English

获取Laravel中的工作表总数

[英]Get total number of sheet in Laravel

I'm trying to get the total number of sheets present in an uploaded Excel file in Laravel. 我正在尝试获取Laravel中上传的Excel文件中存在的工作表总数。 The file I'm uploading has 3 sheets. 我上传的文件有3张纸。 So I'm expecting to get an output of $counter = 3 . 所以我期望得到$counter = 3的输出。

Code: 码:

$counter=0;
Excel::load($fileDetails['file_path'], function($sheet) use($counter) {
    $sheet->each(function($sheet) use($counter) {
        echo "It works</br>";   
        $counter++;
    });
});
echo $counter; exit;

Output: 输出:

It works
It works
It works
0   //-- This is $counter, which is not get incremented. It has to be 3.

The use works like passing parameters. use工作方式类似于传递参数。 The default is to pass by value, so any modifications made inside the function will not be reflected outside the function's scope. 默认值是按值传递,因此在函数内部进行的任何修改都不会反映在函数范围之外。

If you pass the variable by reference, however, the changes will be reflected. 但是,如果通过引用传递变量,则更改将得到反映。

Update your code to: 将您的代码更新为:

$counter=0;
// add & to have var passed by reference
Excel::load($fileDetails['file_path'], function($sheet) use(&$counter) {
    // add & to have var passed by reference
    $sheet->each(function($sheet) use(&$counter) {
        echo "It works</br>"; 
        $counter++;
    });
});
echo $counter;
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM