Search code examples
laravelcsvlaravel-excel

Export csv with laravel excel in specific folder


Hello I am trying to export a csv file in a specific folder, I have tried several things and have no results

By doing the following code it exports me normally in the default /app/ folder

  public function export(){
    $super = Super::select('ano_plantacion','zona','sitio', 'manejo','sup_ha','codigo','rpend')->get();
    Excel::store(new SuperExport($super), 'Super.csv')); 
  }

Im try with this:

Excel::store(new SuperExport($super), 'Super.csv', storage_path('/app/export'));

Error:

"Trying to access array offset on value of type null"

And this:

Excel::store(new SuperExport($super), storage_path('/app/export/Super.csv'));

Error:

Impossible to create the root directory "C:\Users\pachi\Documents\Version_web\mpe_web\storage\app\C:/Users/pachi/Documents/Version_web/mpe_web/storage/app/export

Help pls


Solution

  • Since the Excel store() method uses the default disk if you don't specify another one, which by default in laravel points to storage_path('app'), you can just prepend the export/ directory to the file name:

    Excel::store( new SuperExport($super), '/export/Super.csv' );
    

    Alternatively you can create a disk for the exports by adding the following array under the 'disks' index in the /config/filesystems.php file:

    'disks' => [
        // your other disks here, leave them as they are, and add this one
    
        'export' => [
            'driver' => 'local',
            'root' => storage_path('app/export'),
        ],
    
    ],
    

    And then you specify that disk as the third parameter to the store() method:

    Excel::store( new SuperExport($super), 'Super.csv', 'export' );