SolvedLaravel Excel [BUG] Error import: object beginTransaction

Prerequisites

  • Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • Checked that your issue isn't already filed.
  • Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.2
  • Laravel version: 5.7.17
  • Package version: 3.1.3

Description

I have created the import object class and specified the Concerns of ToCollection. Upon performing Excel::import, I receive an error shown in Actual Behavior below.

Steps to Reproduce

Do not use a transactional database. We use MongoDB.

Aside from that, Model importing was not specified and the package should not be doing any database preparation, that should be handled through events by the developer.

Expected behavior:

dd of the collection rows.

Actual behavior:

1) Tests\Unit\Services\Websites\WebsiteCredentialsImportServiceTest::testImportTabDelimited
Error: Call to a member function beginTransaction() on null

/var/www/vhosts/website.local/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:109
/var/www/vhosts/website.local/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:93
/var/www/vhosts/website.local/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:23
/var/www/vhosts/website.local/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:327
/var/www/vhosts/website.local/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:223
/var/www/vhosts/website.local/vendor/maatwebsite/excel/src/Reader.php:96
/var/www/vhosts/website.local/vendor/maatwebsite/excel/src/Excel.php:120
/var/www/vhosts/website.local/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:223
/var/www/vhosts/website.local/app/Services/Websites/WebsiteCredentialsImportService.php:198
/var/www/vhosts/website.local/app/Services/Websites/WebsiteCredentialsImportService.php:182
/var/www/vhosts/website.local/app/Services/Websites/WebsiteCredentialsImportService.php:122
/var/www/vhosts/website.local/tests/Unit/Services/Websites/WebsiteCredentialsImportServiceTest.php:25

Additional Information

<?php

namespace App\Imports\Websites;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

/**
 * Class WebsiteCredentialsImport
 *
 * @package App\Imports\Websites
 */
class WebsiteCredentialsImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        dd(['COLLECTION' => $rows]);
    }
}
        $collection = Excel::import(
            new WebsiteCredentialsImport,
            $this->file_name,
            null,
            $this->reader_type
        );
14 Answers

✔️Accepted Answer

Maybe you guys are having the same issue I did at #2384

Maybe my solution would work for you, try a:

php artisan config:clear

Sometimes, specially when updating a package, Laravel won't clean the cache and your package won't find the config files. Thus, the driver will be always NULL and fail.

Other Answers:

Next version will have a setting to disable Laravel-DB transactions + a way to add custom transaction handlers

You are probably typing something wrong in your config then. NULL doesn't sound like a valid driver. 'null' (note that it is a string) is.

More Issues: