php - Access a variable outside of Excel::load in Laravel-excel
Get the solution ↓↓↓I'm trying to pass a variable to function below. I tried global variable & by reference but the counters keep resetting (as shown in second patch of code).
private $counter = 0;
public function importExcel(Request $request)
{
$count = 0
**some other code**
try {
Excel::filter('chunk')->load($path)->chunk(1000, function($reader) use($tableName, $tableColumns, &$count) {
var_dump($this->counter);
var_dump($count);
if ($reader->count() && ($this->counter < 30 || $count < 30)) {
$this->counter++;
$count++;
var_dump('inside: ' . $this->counter . ';' . $count);
/** Get uploaded file columns **/
$headerRow = $reader->first()->keys()->toArray();
/** Check if destination table columns are found in uploaded file **/
if (is_array($headerRow) && !array_diff($tableColumns, $headerRow)) {
/** Extract only the data we need in destination table from uploaded file **/
$data = array_map(function($row) use($tableColumns) {
$arr = [];
foreach ($tableColumns as $column) {
$arr[$column] = $row[$column];
}
return $arr;
}, $reader->toArray());
/** Insert the data we need to destination table **/
DB::connection(*censored*)
->table($tableName)
->insert($data);
}
}
});
}
results:
int(0)
int(0)
string(11) "inside: 1;1"
int(0)
int(0)
string(11) "inside: 1;1"
int(0)
int(0)
string(11) "inside: 1;1"
int(0)
int(0)
string(11) "inside: 1;1"
int(0)
int(0)
string(11) "inside: 1;1"
etc... etc... etc...
Why both counters don't accumulate? How can I get the counters to accumulate? corrections or other solutions are welcomed.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: regex stop at first match
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.