Laravel Excel - MaatWebsite/Excel's Import Operation raising DB Connection not Configured error

1.5k views Asked by At

I am using Laravel 6.2 and Maatwebsite/Excel 3.1 here I am trying to import a excel file and willing to get data present in it in an Array format and in the Controller I need to validate some data among them and along with some other fields i need to insert that excel fetched array data to the database. Laravel application has no direct database connection. I am calling an API to insert those fields to the database, but the import operation giving Database connection [] not configured. error. Help me to sort out actual issue from it.

Controller Code

public function importExcelData(Request $request) {
   // dd($request->excelFile);
   $data = Excel::import(new UsersImport, request()->file('your_file'));
    dd($data);
}

UsersImport File

<?php

namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToArray;

class UsersImport implements ToArray 
{
    public function Array(Array $tables)
    {
        return $tables;
    }
}
?>

enter image description here

2

There are 2 answers

0
Kartik Bhat On BEST ANSWER

After following the error stack appeared in the browser console's network response I got that I can solve this error very easily. as per Laravel Excel Documentation enter link description here we need to run this command in a command prompt

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"

after executing this command 'excel.php' file got created under config folder of the application

in this file , we can found this code

 'transactions' => [
        'handler' => 'db',
 ],

we need to change it as

 'transactions' => [
        'handler' => 'null',
 ],

Now after dumping the import result using dd(), I am seeing those data present in that excel file.

enter image description here

enter image description here

0
a3rxander On

You have to specific database connection in your config\excel.php file, you need to declare it under the db key. You can choose from one of the connections already defined in your config/database.php file.

For instance, if you have an Oracle connection declared in config/database.php, you can use it in your config\excel.php file like this:

'transactions' => [
    'handler' => 'db',
    'db'      => [
        'connection' => 'oracle',
    ],
],