Maatwebsite Excel导入失败时如何显示错误消息

问题描述

我尝试使用Maatwebsite-3.1和Laravel-5.8进行excel导入:

public function import(Request $request){
    $request->validate([
        'file' => 'required|max:10000|mimes:xlsx,xls',]);

    $path = $request->file('file')->getRealPath();

    try{

        Excel::import(new StudentsImport,$path);
        
    } catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
        $failures = $e->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.
        }
    }
    
    return back()->with('status','Students are added successfully!');
}

class StudentsImport implements WithMultipleSheets
{
    public function sheets(): array
    {
        return [
            new FirstStudentSheetImport()
        ];
    }
}

class FirstStudentSheetImport implements OnEachRow,WithheadingRow
{
    public function onRow(Row $row)
    {   
        $rowIndex = $row->getIndex();

        if($rowIndex >= 200)
            return; // Not more than 200 rows at a time

        $row = $row->toArray();

        $student_info = [
            'student_id'           => $tb->id,'birthday'             => $row['birthday']?? date('Y-m-d'),'religion'             => $row['religion'] ?? '','first_name'          => $row['first_name'],'last_name'  => $row['last_name'] ?? '','user_id' => auth()->user()->id,];
        
        create(StudentInfo::class,$student_info);
    }
}

导入成功后,我会收到成功消息,但导入失败时,我会看到错误500

如何使应用程序显示失败的错误消息,而不是错误500?

谢谢

解决方法

我遇到了同样的问题。 在您的导入类中,您需要实现 SkipsOnError,SkipsOnFailure

你的类应该是这样的:

class xxxImport implements ToModel,WithStartRow,WithValidation,SkipsOnError,SkipsOnFailure 
{
     use Importable,SkipsErrors,SkipsFailures;

有了这个,您可以从控制器中捕获错误 注意可以替换

Excel::import(new xxxImport,$request->file('file')); 

 $import =  new formationsImport;

现在所有的失败都可以在这里找到:

$import->failures() 
<?php

namespace App\Imports;

use Throwable;
use App\xxx;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithValidation;

class xxxImport implements ToModel,SkipsFailures;
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Formation([
            //
            'name' => $row[0],'description' =>$row[1],//etc
        ]);
    }

    /** Import startRow
     * @return int
     */
    public function startRow(): int
    {
        return 2;
    }

    public function rules():array   
    {
        # code...
        return [
            '*.0' => ['required','unique:formations,name'],'*.1' => ['regex:/(^[^=\-+\/\\\*%])+/','required'],'*.2' => ['regex:/(^[^=\-+\/\\\*%])+/','*.3' => ['required'],'*.4' => ['required'],'*.5' => ['required'],'*.6' => ['required'],'*.7' => ['required'],'*.8' => ['required'],'*.9' => ['required'],'*.10' => ['required'],'*.11' => ['required'],'*.12' => ['required'],'*.13' => ['required'],'*.14' => ['required'],'*.15'  => ['required',slug'],];
    }

    public function customValidationMessages()
        {
            return [
                '0.unique' => 'Le nom de la formation existe déjà','15.unique' => 'Le slug de la formation existe déjà',];
    }

     public function onError(Throwable $error)
     {
        
     }

    // public function onFailure(Failure ...$failures) // 
    // {
            
    // }

你的控制器应该是这样的。

public function handleImport(Request $request)
    {
        //
            if (empty($request->file('file'))) 
            {
                return back()->with('error','custom message');
            }
            else{   
                request()->validate([
                    'file'  => 'required|mimes:xls,xlsx,csv|max:2048',]);

                $pathTofile = $request->file('file')->store('xxx','public');
                
                // Excel::import(new xxxImport,$request->file('file')); 
                $import =  new xxxImport;
                $import->import($pathTofile); // we are using the trait importable in the xxxImport which allow us to handle it from the controller directly

                // dd($import->failures());
                    if($import->failures()->isNotEmpty()){
                        $failures = $import->failures();
                        
                        return view('admin.formations.failures')->with('failures',$failures);
                    }
                return back()->with('success','Formations importées avec succès');
            
            }
    }

    ```