Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm following this example from the docs to handle multiple sheets import.

My Controller:

    public function import(Request $request) {
    
        $file = $request->file('import')->store('/storage');
        $import = new MultisheetContactsImport();        
        $import->import($file);            
    
   
        if ($import->failures()->isNotEmpty()) {
            return $import->failures();
        } 

        return $import->getRowCount();           
    }

My import class ContactsImport.php

namespace AppImports;

use AppModelsEmail;
use MaatwebsiteExcelConcernsToModel;
use MaatwebsiteExcelConcernswithHeadingRow;
use MaatwebsiteExcelConcernsSkipsOnError;
use MaatwebsiteExcelConcernsSkipsErrors;
use MaatwebsiteExcelConcernsImportable;
use MaatwebsiteExcelConcernsWithValidation;
use MaatwebsiteExcelConcernsSkipsOnFailure;
use MaatwebsiteExcelConcernsSkipsFailures;
use IlluminateSupportStr;
use IlluminateSupportCollection;
use MaatwebsiteExcelConcernsToCollection;
use IlluminateSupportFacadesValidator;
use Throwable;
class ContactsImport implements ToModel, withHeadingRow, SkipsOnError, WithValidation, SkipsOnFailure
{      
    private $rows = 0;

    use Importable, SkipsErrors, SkipsFailures;    

    /**
    * @param array $row
    *
    * @return IlluminateDatabaseEloquentModel|null
    */
    public function model(array $row)
    {
    ++$this->rows;

    return new Email([
        'apiKey' => Str::random(16),
        'firstname' => $row['firstname'],
        'lastname' => $row['lastname'],
        'emailAddress' => $row['emailaddress'],
        'businessType' => $row['businesstype'],
        'allowed' => true,
        'updates' => true,
        'marketing' => true,
    ]);       
    }

    public function getRowCount(): int
    {
        return $this->rows;
    }    

    public function rules(): array 
    {
        return [
            '*.emailaddress' => ['email', 'unique:emails,emailaddress']
        ];
    }     
}

My Multisheet import class MultisheetContactsImport.php

class MultisheetContactsImport extends ContactsImport implements WithMultipleSheets
{  
    public function sheets(): array
    {
        return [
            'Contacts' => new ContactsImport()
        ];       
    }    
}

The methods failures and and getRowCount work fine if I use the ContactsImport class without multisheets however now I only get a response 0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
546 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just Wrap your import line code with try catch block it should handle every thing:

     try {
        $master = Excel::import(new MasterImport($auth), $request->file('master_upload'));
    } catch (MaatwebsiteExcelValidatorsValidationException $e) {
        $failures = $e->failures();
        dd($failures);
         
         foreach ($failures as $failure) {
             $failure->row(); // row that went wrong
             $failure->attribute(); // either heading key (if using heading row concern) or column index
             $failure->errors(); // Actual error messages from Laravel validator
             $failure->values(); // The values of the row that has failed.
         }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...