Skip to content
Advertisement

Laravel: How to store Excel File in a given path?

I have a function which is converting my blade view into Excel format and downloading it. Now I want to save it in a specified location, i.e: public/uploads/release Here is my controller:

 public function export_release()
{
    return Excel::download(new ReleaseExportView(), 'release.xlsx');
}

This function downloads the excel file downloads folder. I want to save it in the location: public/uploads/release

Here is my route:

Route::Get('outbounds/export_release' ,'outboundController@export_release')->name('outbounds.export_release');

Advertisement

Answer

You can use the store method to store the file in the filesystem.

From the docs:

Exports can easily be stored on any filesystem that Laravel supports.

Default disk

public function storeExcel() 
{
    // Store on default disk
    Excel::store(new InvoicesExport(2018), 'invoices.xlsx');
}

Custom disk

public function storeExcel() 
{
    // Store on a different disk (e.g. s3)
    Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3');
    
    // Store on a different disk with a defined writer type. 
    Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3', Excel::XLSX);
}

Now ideally you would store the file in storage/app/public/uploads/release and create a symlink from public/storage/ to storage/app/public as explained here.

But if you really want to store it in public/uploads/release you could create new custom disk in config/filesystems.php. Something like:

'real_public' => [
    'driver' => 'local',
    'root' => public_path(),
    'url' => env('APP_URL'),
    'visibility' => 'public',
],

Then you could use the custom disk to store the file:

public function export_release()
{
    Excel::store(new ReleaseExportView(), 'uploads/release/release.xlsx', 'real_public');
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement