Home > other >  Download button does the same functionality as filter button in Laravel
Download button does the same functionality as filter button in Laravel

Time:10-25

I have a view in my Laravel project that lets me filter my records then I have the option to download the records to excel but when I click download, it ends up filtering the records instead of exporting to excel.

Controller:

 public function exportvehicles() 
    {
        return Excel::download(new VehicleLog, 'users.xlsx');
    }

Model:

public function collection()
    {
        return VehicleLog::all();
    }

View:

 <button  class="btn btn-primary">Export to Excel</button>

Routes:

  Route::get('users/export/', 'ReportController@exportvehicles');

CodePudding user response:

Probable, you send post request for download process. Since the filter button is also between the form tags, it is also seen as the submit button. For this reason, you need to write type = "submit" in the upload button and type = "button" in the filter button. If you share html codes, I can answer more correctly.

CodePudding user response:

If you did not use ajax or something. Try this.

<a href="{{"/users/export/"}}" target="_blank" class="btn btn-primary">Export to Excel</a>

CodePudding user response:

In Vehicle Controller

Use Maatwebsite\Excel\Facades\Excel;
use App\Exports\VehicleExport;
        Use App\VehicleLog;
        
       public function exportvehicles(){
       $vehicle = VehicleLog::all();
       $data = [
       'success' => 'success'
       'vehicles' => $vehicle
       ];
       return Excel::download(new VehicleExport($data), 'Vehicledata.xlsx');
       }

and in VehhicleExport.php

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

public function view(): View
{
    //dd($this->data);   
    return view('Spreadsheets.Vehicle_data',$this->data);
}

after this you have to make balde part as how you want to export

  • Related