Laravel: 如何在指定路径下存储Excel 文件?

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

我有一个功能,可以将我的 blade 视图转换为 Excel 格式并下载。现在我想把它保存在指定的位置,即:public/uploads/release 这是我的控制器:

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

此函数下载 excel 文件下载文件夹。我想保存在位置:public/uploads/release

这是我的路线:

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

您可以在config/filesystems中创建新的存储光盘。php:

'excel_uploads' => [
    'driver' => 'local',
    'root'   => public_path() . '/uploads/release',
],

并像这样存储文件:

Storage::disk('excel_uploads')->put($path, $file_content)

您可以使用store方法将文件存储在文件系统中。

来自docs

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

默认磁盘

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

自定义磁盘

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);
}

现在理想情况下,您可以将文件存储在 storage/app/public/uploads/release 中,并创建一个从 public/storage/storage/app/public 的符号链接,如 here 所述。

但如果您真的想将其存储在 public/uploads/release 中,您可以在 config/filesystems.php 中创建新的自定义磁盘。类似于:

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

然后你可以使用自定义磁盘来存储文件:

public function export_release()
{
    Excel::store(new ReleaseExportView(), 'uploads/release/release.xlsx', 'real_public');
}