0

I have maatwebsite Excel package to import an Excel document. The Excel document has multiple sheets, So i have a import class which defines the other import classes to use for each sheet.

All the importing and validation works fine, up to the point where i want to skip on errors and failures and collect them all at the end.

As i understand it, an array for errors and failures are add to the import class through the traits, and these get populated as the import happens.

Problem is, the arrays are on each sheet import class not the main import class that defines the sheets, so i cant access the arrays.

php7.3, laravel8, maatwebsite/excel 3.1,

class MainImport implements WithMultipleSheets, WithProgressBar
{

use Importable


/**
 * Define schema classes for each sheet in the excel document.
 * @return array
 */
public function sheets(): array
{
    return [
        'first sheet' => new FirstSheetImport(),
        'second sheet' => new SecondSheetImport()
    ];
}


}

Import class for sheets, second would be some thing similar but own validation rules, own model etc

class FirstSheetImport implements ToModel, WithHeadingRow, WithValidation, SkipsOnError, SkipsOnFailure
{
use SkipsFailures, SkipsErrors;


public function rules(): array
{
    return [
        'name' => 'required'
    ];
}

/**
 *
 * @param array $row
 * @return Model
 */
public function model(array $row)
{

    Model::updateOrCreate([
        'id' => $row['asset_id']
    ],[
        'address' => $row['address'],
        'name'  => $row['name'],
        'carrier_name' => $row['cp_name'],
    ]);
}
}

This is the basic idea behind the logic

$import = new MainImport();

$import->import(file.xlsx); // so MainImport is actually using FirstSheetImport etc to import each sheet

if ($import->errors()) {
    //do something with the errors from each sheet - but arrays not on MainImport
    // same with failures
    // dont seem to be able to reference the sheet import classes to get to the arrays either

}

I cant see how i would capture the failures/errors from each sheet import on the MainImport.

Any ideas how to solve this, or restructure the logic?

1 Answer 1

0

Solved it. just instantiate the sheet classes directly on the MainImport instead of at call on the sheets method

class MainImport implements WithMultipleSheets, WithProgressBar
{
use Importable;

protected $schema = [];

public function __construct()
{
    $this->schema['first sheet'] = new DefaultSheetImport();
}


/**
 * Define schema classes for each sheet in the excel document.
 * @return array
 */
public function sheets(): array
{
   return $this->schema;
}

of course now i can get to the sheet classes - dont know why i didnt think of that before! Doh!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.