Replies: 1 comment 1 reply
-
So this worked... but not sure if it's sensible practice: Import <?php
namespace App\Imports;
use App\Models\Service\ContractScheduledCheck;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ContractScheduledChecksImport implements ToArray, WithHeadingRow
{
use Importable;
private $parsedRows = [];
private $rowCount = 0;
public function getParsedRows()
{
return $this->parsedRows;
}
/**
* @param array $row
*
* @return ContractScheduledCheck|null
*/
public function array(array $rows)
{
$parsedRows = [];
foreach ($rows as $index => &$row)
{
$rowArray = [];
try {
$rowArray = [
'id' => $this->rowCount,
'error' => null,
'contract_id' => preg_replace("/[\D]/",'', $row['contract_number']),
'perform_before' => $row['perform_before'],
'booked_on' => $row['booked_on'],
'performed_on' => $row['performed_on'],
'notes' => $row['notes'],
'original_row' => $row,
];
} catch (\Exception $e) {
$rowArray = [
'id' => $this->rowCount,
'error' => $e->getMessage(),
'contract_id' => '',
'perform_before' => '',
'booked_on' => '',
'performed_on' => '',
'notes' => '',
'original_row' => $row,
];
}
array_push($parsedRows, $rowArray);
$this->rowCount = $this->rowCount + 1;
}
array_push($this->parsedRows, ...$parsedRows);
return $rows;
}
} Controller
if ($request->has('step') && $request->input('step') === 'validate') {
$importer = new ContractScheduledChecksImport();
$rows = $importer->import(
request()->file('schedule_import_file'),
null,
\Maatwebsite\Excel\Excel::XLSX
);
$parsedRows = $importer->getParsedRows();
return [
'data' => $parsedRows
];
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm trying to do an import dry-run for validation and to provide feedback to the user for correction of errors in the frontend before final import. Is there something dumb I'm doing here that is breaking this?
One of two things happens depending on how I run the import:
This way returns the raw spreadsheet array:
Returns
This way calls array() on the import but returns the chainable Excel static class, not the data
returns
my Import class: ContractScheduledChecksImport
Beta Was this translation helpful? Give feedback.
All reactions