How to remove double quotations from exported csv file

Viewed 816

I'm using Laravel 5.8 and I have added Maatwebsite package for exporting CSV files from a database table.

And here is my exported class:

class ConfirmedExport implements FromCollection, WithHeadings
{
    public function headings():array{
        return [
   
        ];
    }

    public function collection()
    {
        return collect(WithdrawWallet::getData());
    }
}

And the result looks like this:

"123456789","2100","Desc","lname","fname"

But I need to remove double quotations (" ") from the words, so the expected result looks like this:

123456789,2100,Desc,lname,fname

So how to do that?


UPDATE #1:

I just tried this code for Export Class:

namespace App\Exports;

use App\WithdrawWallet;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ConfirmedExport implements FromCollection, WithHeadings
{
    public function headings():array{
        return [
   
        ];
    }

    public function collection()
    {
        return collect(WithdrawWallet::getData());
    }
    
    public function getCsvSettings(): array
    {
        return [
            'enclosure' => ''
        ];
    }
}

And it successfully removes the " " from the last element (which is: 123456789), but still shows the " " for the other ones!

enter image description here

1 Answers

@nagidi after you publish the config you may change this in the settings.

  1. Run following command:

    php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
    
  2. Remove " (double quote) from 'enclosure' in config/excel.php

     'exports' => [
         'csv' => [
             'enclosure' => '', // was '"'
             // other settings
         ],
     ],
    

Now your csv export values doesn't contain the " enclosure anymore.

If you want to change this setting per Export, you can implement the WithCustomCsvSettings Concern and add the getCsvSettings method to your ConfirmedExport class:

use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;

class ConfirmedExport implements WithCustomCsvSettings
{
    // other code

    public function getCsvSettings(): array
    {
        return [
            'enclosure' => ''
        ];
    }
}
Related