Laravel-excel: How to get a variable out after Excel::load process

Created on 28 Jan 2018  路  2Comments  路  Source: Maatwebsite/Laravel-Excel

I have a variable just to count no of rows, but I may skip some NULL rows, so I need to get the variable "$count_rows" out after insert the rows in Database

    $count_rows = 0;
    $excel = Excel::load('uploads/file.xlsx;, function($reader) use ($count_rows) {
        $results = $reader->get();
        foreach ($results as $row)
        {
            if(!$row->Code6) {
                continue;
            }
            $data = $row->toArray();
            $record_mported = Export::create($data);
            $count_rows++;
        }
    });
    return $count_rows ;

Now $count_rows still equal to 0;
How to return it after Excel::Load process.

Most helpful comment

Or don't use a closure:

$reader = Excel::load('uploads/file.xlsx');

All 2 comments

Usually if you pass a variable by reference, it does the job you need to add '&' sign before the variable to do so, so your line will be like this.

$excel = Excel::load('uploads/file.xlsx', function($reader) use (&$count_rows) {

But the same isn't working for Excel::filter, i dont know why, see if it works and do message if it does.
Also i see you have added ';' sign in the end of file, instead of a quote, do correct it as well.

Or don't use a closure:

$reader = Excel::load('uploads/file.xlsx');

Was this page helpful?
0 / 5 - 0 ratings