0

I'm facing an issue when import data with the excel laravel package. Eg: In my import file have 5 items with 3 item error after use validation I want to skip there items and continue import 2 items remain. currently if in the file have any error it will stop import process. Any solution for this issue?

1 Answer 1

0

Sometimes you might want to skip all errors, e.g. duplicate database records. By using the SkipsOnError concern, you get control over what happens the moment a model import fails. When using SkipsOnError the entire import will not be rollbacked when an database exception occurs.

   <?php
    
    namespace App\Imports;
    
    use App\User;
    use Maatwebsite\Excel\Concerns\ToModel;
    use Maatwebsite\Excel\Concerns\Importable;
    use Maatwebsite\Excel\Concerns\SkipsOnError;
    use Maatwebsite\Excel\Concerns\WithValidation;
    
    class UsersImport implements ToModel, WithValidation, SkipsOnError
    {
        use Importable;
    
        /**
         * @param \Throwable $e
         */
        public function onError(\Throwable $e)
        {
            // Handle the exception how you'd like.
        }
    }

If you automatically want to skip all exceptions and collect them at the end of the import, you can use the Maatwebsite\Excel\Concerns\SkipsErrors trait

    <?php

namespace App\Imports;

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsErrors;

class UsersImport implements ToModel, WithValidation, SkipsOnError
{
    use Importable, SkipsErrors;
}

For further Information read This

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.