Laravel-excel: Validating All Sheets on Import

Created on 9 Jul 2019  路  2Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions

  • PHP version: 7.1.28
  • Laravel version: 5.6.39
  • Package version: 3.1.13

Description

I've got an import class which has multiple sheets; I'm performing validation within the array() function manually. If there are any validation issues with the first sheet, it just throws the ValidationException and so I can only see issues with the first sheet. Is there anything I can do to continue onto the next sheet even if the first fails?

question

Most helpful comment

public function collection(Collection $rows)
    {
         try {
Validator::make($rows->toArray(), [
             '*.0' => 'required',
         ])->validate();
} catch(ValidationException $e)
{
    // store validation errors somewhere and get them at the end of the validation via a getter
}

        foreach ($rows as $row) {
            User::create([
                'name' => $row[0],
            ]);
        }
    }

(extended version of https://docs.laravel-excel.com/3.1/imports/validation.html#row-validation-without-tomodel; handle the try-catch yourself)

All 2 comments

public function collection(Collection $rows)
    {
         try {
Validator::make($rows->toArray(), [
             '*.0' => 'required',
         ])->validate();
} catch(ValidationException $e)
{
    // store validation errors somewhere and get them at the end of the validation via a getter
}

        foreach ($rows as $row) {
            User::create([
                'name' => $row[0],
            ]);
        }
    }

(extended version of https://docs.laravel-excel.com/3.1/imports/validation.html#row-validation-without-tomodel; handle the try-catch yourself)

Good idea, will give that a go.

Was this page helpful?
0 / 5 - 0 ratings