Search code examples
phplaravelexportexport-to-excelmaatwebsite-excel

Laravel: export Excel File in a given path?


Api

Route::get('UserExport', function (UserRequest $request) {
    return Excel::download(new UserExportExcel($request->id), 'UserReport.xlsx');
});

UserExportExcel

 private $id;

    public function __construct(int $id)
    {
        $this->id = $id;
    }


    public function collection()
    {
        $filePath = storage_path('app/Users/'.$this->id . '.xlsx'); //file name is 15.xlsx which is the id +.xlsx
        User::all()->downloadExcel(
            $filePath,
        );
    }

I'm trying to export my excel file without using model!


Solution

  • Done I solve it using this method directly from API (i specify the full path of my excel file (path+name),and downloaded using Response::download

    use Illuminate\Support\Facades\Response;
    
    Route::get('UserExport', function (UserRequest $request) {
        $filePath = storage_path('app/Users/' . $request->id . '.xlsx');
        return Response::download($filePath);
    });