Fix inefficiencies while looping through CSV in ImportController
MODIFY
app/Http/Controllers/ImportController.php
app/Http/Controllers/ImportController.php
@@ -42,15 +42,12 @@ class ImportController extends Controller {
$headers = [];
$file = fopen(storage_path('app/imports/' . $import->file), 'r');
+ $firstRow = fgetcsv($file, 999, ',');
- while ($row = fgetcsv($file, 999, ',')) {
- foreach ($row as $column) {
+ foreach ($firstRow as $column) {
$headers[] = $column;
}
- break;
- }
-
return view('imports.prepare', compact('headers'));
}
@@ -90,10 +87,10 @@ class ImportController extends Controller {
'description' => $row[$import->column_description],
'amount' => $row[$import->column_amount]
];
- }
-
+ } else {
$heading = false;
}
+ }
return view('imports.complete', compact('rows'));
}

Fix inefficiencies while looping through CSV in ImportController