Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincentsanity's avatar

Generate excel based on the query from database using vue and laravel maatwebsite package

Basically I got a query to get all the record based on the date from and to. I use maatwebsite package in laravel. The problem is if i click the generate button it will not generate an excel file. Im using version 2.x of this package because it is the stable one. Can someone help me with this? I just only want to print the results of the query in the excel format. I wont change the version to 3.x because if i do that, the import functions of the other component wont work for version 3.x . Thanks a lot

My controller, dont mind the dd just for test purposes

 public function generateReport(Request $request){

        $date = \DB::table('checkers')
        ->where('remarks_id',2)
        ->join('schedules','schedules.id','=','checkers.schedule_id')
        ->join('teachers','schedules.teacher_id','=','teachers.id')
        ->join('subject_codes','subject_codes.id','=','schedules.subject_code_id')
        ->join('remarks','remarks.id','=','checkers.remarks_id')
        ->where('checkers.created_at', '=>', $request->from)
        ->where('checkers.created_at', '=<', $request->to)
        // ->whereBetween('checkers.created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
        ->select('teachers.fullname','subject_codes.subject_description','remarks.remarks_desc','checkers.created_at')
        ->get(); 

        dd($request->from);

        return Excel::download('users.xlsx');
    }

If i hit the generate button i got error of Laravel Excel method [download] does not exist

Another problem for this is even if i dd the request, it returns null i dont even know what is happening, i got v-model in vue to get the request still cant show.

My vue component

 <form action="">
                   <div class="col-xs-4 form-group">
                        <label>Start Date</label>
                       <date-picker name="from" id="from" v-model="from" :config="options" ></date-picker>
                    </div>
                    <div class="col-xs-4 form-group">
                        <label>End Date</label>
                          <date-picker name="to" id="to" v-model="to" :config="options" ></date-picker>
                    </div>
               </form>
            </div>
            <div class="box-footer">
                  <a type="button" href="/generate" >
                    <button  class="btn btn-info">
                        Generate Excel
                    </button>
                </a>
            </div>

The script, in the routes.php, the route for generating is /generate and it points to the controller method generateReport

  data(){
            return{
                    from: new Date(),
                    options: {
                    format: 'YYYY-MM-DD',
                    showClear: true,
                    showClose: true,
                    },

                    to: new Date(),
                    options: {
                    format: 'YYYY-MM-DD',
                    showClear: true,
                    showClose: true,
                    } 
            }
        },
        created() {
            console.log('Component mounted.')
        },
        methods:{
            generate(){
                axios.get('/generate')
                    .then((res)=>{
                        console.log('asd')
                    })
            }
        }
0 likes
6 replies
bobbybouwmann's avatar

If you open the /generate URL the download should probably just work. This works because you send back a download response. However, axios doesn't understand the download response it gets back from your controller.

You have two options two work around this. Handle the download response in Vue and generate a browser download response there.

The other option is opening the generate URL in a new tab from your Vue, which will trigger the download response automatically. Code might look like this

generate() {
    window.open('/generate');
}

I would personally go for option 2 ;)

Note: Make sure that opening the /generate page works outside your JS. If that works option 2 should be perfect in your case;0

2 likes
The-skeptic 's avatar

Greetings friend, you know how to export excel the result of a search with scope and form input

vincentsanity's avatar

okay sir but how could I pass the params from and to because if I dd them in the controller, the response is null @bobbybouwmann

sriwebdev's avatar

Hi sir, Im already doing method 2, but the problem is Im handling API Authorization using token, but I cannot inmplement that in routes for Exports

Please or to participate in this conversation.