Search code examples
laravellaravel-bladelaravel-5.7laravel-excel

how to send hidden file to view of laravel (5.7)


I am creating an import function for import an excel file. the controller will handle it and save data to database.and after that,i want export the error to an excel file and send it to view. If the user clicks download, the excel file contains the error will download, otherwise do nothing.

So, I want to send a hidden file to the view to avoid saving the file to the database.

how to do it, please help!


Solution

  • after handle import in controller, you can pass error data to an array and send it to session like this:

    $request->session()->put('err_file', $arr_err_data);
    

    and create function to get error file :

     public function getErrFile(){
            return Excel::create('Error', function ($excel){
                $excel->sheet('Error', function ($sheet) {
                    $arr_err=Session::get('err_file');
                    $sheet->fromArray($arr_err);
                });
            })->download('xlsx');
        }
    

    Dont forget create route for this function:

    Route::get('/get_err_file', 'YourController@getErrFile');
    

    Final, you can create link to download it:

    <a class="btn btn-link" href="{{url('get_err_file')}}" >Download</a>
    

    I hope it can help you!