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

Josadec's avatar

Laravel view

Good Afternoon for all,

I need help with a form where users put application numbers to be able to generate Excel report. the user knows the application number for example they need start the application 1 to application 10 my problem is how can receive this information to came from the form and than receive in the Laravel Excel FromView

Livewire Form

<?php

namespace App\Http\Livewire\Examsreports;

use Livewire\Component;

class FormExamsReports extends Component
{
    public $fromExam = 1, $toExam = 10;

    public function render()
    {
        return view('livewire.examsreports.form-exams-reports');
    }
}

View Component

<div>
    <div class="container">
        <div class="px-10">
            <form action="" class="w-full max-w-lg" >

                <div class="flex flex-wrap -mx-3 mb-6">
                    <div class="w-full md:w-1/2 px-3 mb-6 md:mb-0">
                        <div class="grid grid-cols-1 divide-y">
                            <div class="py-5">
                               
                            </div>
                            <div class="py-5">
                                <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="">De examen: </label>
                                    <x-input wire:model="fromExam" type="number" class="w-100 w-full" placeholder="Solo numeros" /> 
                                    {{-- @error()
                                        <div class="">{{ $message }}</div>
                                    @enderror --}}
                                <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="">A examen:</label> 
                                    <x-input wire:model="toExam" type="number" class="w-full" placeholder="Solo numeros" />
                            </div>
                           {{--  <div class="py-5">
                                <x-label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for=""> De fecha:</x-label> 
                                <x-input type="date" class="min-w-full" /> 
                                <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for=""> A fecha:</label> 
                                <x-input type="date" class="w-full" />
                            </div> --}}
                            <div class="py-5">
                                <a href="{{ route("reportExams",$fromExam,$toExam) }}">
                                    <x-danger-button >
                                        Ver Reporte &rarr;  <i class="far fa-clipboard"></i>
                                    </x-danger-button>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </form>     
        </div>
    </div>
   
</div>

Rotue

Route::get('/export/reports/{countperanswers}',[ExportExcelController::class,'countAnswerPerExams'])->name('reportExams');

Class ExportExcelController

  public function countAnswerPerExams ()
    {
        $fecha = date('m_d_Y');
        $filename = "ApplicationReport_";
        return Excel::download(new AnswersReport, $filename.$fecha.'.xlsx');
    }

Export to excel FromView

<?php

namespace App\Exports;

use App\Models\Answer;
use Illuminate\View\View;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromView;

class AnswersReport implements FromView
{
    use Exportable;

        public $fromExam = 5, $toExam = 10;

        public function view (): View
        {
            $answers = Answer::join('questions', 'answers.question_id', '=', 'questions.id')
            ->join('answer_application', 'answer_application.answer_id', '=', 'answers.id')
            ->selectRaw('questions.id AS question_id, questions.description AS question_name,
                                answers.description AS answer_name,
                                count(*) as total')
            ->groupByRaw('answers.id, questions.id, questions.description, answers.description')
            ->Where('answer_application.application_id','>=',5)
            ->Where('answer_application.application_id','<=',10)
            ->orderby('question_id')
            ->get(); 
             
            $preguntas = [];

            foreach ($answers as $answer) {
                $preguntas[$answer->answer_name][$answer->question_id] = $answer->total;
            }

            $data['answers'] = $answers;
            $data['preguntas'] = $preguntas;
     
             return view('exports.excel.reportperanswer',$data);
        }
   
}

View to Export

<table>
    <thead>
    <tr>
        <th>#</th>
        <th>Pregunta</th>
        <th>Siempre</th>
        <th>Casi Siempre</th>
        <th>Algunas veces</th>
        <th>Casi nunca</th>
        <th>Nunca</th>
    </tr>
    </thead>
    <tbody>
    @php
        $preQuestion=null;
    @endphp
    
    @foreach($answers as $answer)
        @if($preQuestion!=$answer->question_id)
            @php $preQuestion=$answer->question_id; @endphp
            <tr>
                <td>{{ $answer->question_id }}</td>
                <td>{{ $answer->question_name."?" }}</td>
                <td>{{ $preguntas['Siempre'][$answer->question_id]??0}}</td>
                <td>{{ $preguntas['Casi siempre'][$answer->question_id]??0}}</td>
                <td>{{ $preguntas['Algunas veces'][$answer->question_id]??0}}</td>
                <td>{{ $preguntas['Casi nunca'][$answer->question_id]??0}}</td>
                <td>{{ $preguntas['Nunca'][$answer->question_id]??0}}</td>
            </tr>
        @endif

    @endforeach

    </tbody>
</table>

If I need to do this code but using other way, could help me an example?

Thank you

0 likes
2 replies
vincent15000's avatar

You have to pass the numbers as parameters of the route which will export the PDF.

You have to place the inputs where you can write the numbers inside a form and submit the form via a POST request, so you can retrieve the numbers from and to in your controller.

public function export(Request $request)
{
	$from = $request->from;
	$to = $request->to;
	// do something
}
Josadec's avatar

@vincent15000 Thanks for your help, could you help me with a better example? If you could use my code it's going be better.

Please or to participate in this conversation.