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

Josadec's avatar

Livewire pass parameters to laravel excel by form

I need help guys, in my form, I use the jetstream component in livewire need send those variables to my export FromQuery, but I do not have an idea how can make it... somebody could help me? please

my code.

Livewire Class

<?php

namespace App\Http\Livewire\Reports;

use App\Models\Branch;
use App\Models\Status;
use Illuminate\Validation\Rule;
use Livewire\Component;

class Index extends Component
{
    public $doneby="";
    public $branch="";
    public $status="";
    public $adatefrom="";
    public $adateto="";
    public $cdateform="";
    public $cdateto="";


   public function updated()
   {
        $this->validate([   
            'doneby' => ['max:3','nullable',
            Rule::exists('dailies','done_by')->where('done_by',$this->doneby)  
            ],
            'branch' => 'nullable',
            'status' => 'nullable',
            'adatefrom' => 'date',
            'adateto' => 'date',
            'cdateform' => 'date',
            'cdateto' => 'date'
    ]);
   }

    protected $validationAttributes =[
        'doneby' => 'Done By',
        'status' => 'Status',
        'adatefrom' => 'From date',
        'adateto' => 'To Date',
        'cdateform' => 'From Date',
        'cdateto' => 'To Date'
    ]; 

    public function generate ()
    {
       
    }

    public function render()
    {
        $branches = Branch::all();
        $statuses = Status::all();

        return view('livewire.reports.index',compact('branches','statuses'));
    }
}

---- view livewire ---

<div>
    
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Reports to Excel') }}
        </h2>
    </x-slot>

    <div class="max-w-7xl mx-auto py-10 sm:px-6 lg:px-8">
        <x-form-section submit="">
            <x-slot name="title">
                {{ __('Osto daily reports ') }}
            </x-slot>
            <x-slot name="description">
                {{ __('Export Osto daily to excel multiples reports') }}
            </x-slot>

            <x-slot name="form">
                <div class="col-span-6 sm:col-span-4">
                    <x-label for="doneby" :value="__('Done By: ')"></x-label>
                    <x-input wire:model='doneby' type="text" name="" id="doneby" class="w-full uppercase"></x-input>
                    @error('doneby')
                        <div class="">{{ $message }}</div>
                    @enderror
                </div>

                <div class="col-span-6 sm:col-span-4">
                    <x-label for="branch" :value="__('Branch: ')"></x-label>
                    <select wire:model='branch' name="" id="branch" class=" w-full block border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm">
                        <option value=""></option>
                        @foreach ($branches as $branch)
                            <option value="{{ $branch->short_name }}">{{ $branch->name }}</option>
                        @endforeach
                    </select>
                    @error('branch')
                        <div class="">{{ $message }}</div>
                    @enderror
                </div>

                <div class="col-span-6 sm:col-span-4">
                    <x-label for="status" :value="__('Status: ')"></x-label>
                    <select wire:model='status' name="" id="status" class=" w-full block border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm">
                        <option value=""></option>
                        @foreach ($statuses as $status)
                            <option value="{{ $status->type }}">{{ $status->type }}</option>
                        @endforeach
                    </select>
                    @error('status')
                        <span class="">{{ $message }}</span>
                    @enderror
                </div>

                <div class="con-span-6 sm:col-span-4">
                    <x-label for="assignedate" :value="__('Assigned Date: ')"></x-label>
                    <span class="text-sm text-gray-300">From: </span><x-input wire:model="adatefrom" type="date" name="" id="assiggnedate" class=""></x-input>
                    <span class="text-sm text-gray-300">To: </span><x-input wire:model="adateto" type="date"></x-input>
                </div>

                <div class="con-span-6 sm:col-span-4">
                    <x-label for="fromcompleted" :value="__('Completed By: ')"></x-label>
                    <span class="text-sm text-gray-300">Form: </span><x-input wire:model="cdateform" type="date" name="" id="fromcompleted" class=""></x-input>
                    <span class="text-sm text-gray-300">To: </span><x-input wire:model="cdateto" type="date" name="" id="" class=""></x-input>
                </div>
                <x-slot name="actions">
                    <div class="flex space-x-4">
                       <a href="{{ route('report.parameters',['doneby' => $doneby]) }}">
                            <x-button>
                                {{ __('Generate ') }}
                                <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M6 2a2 2 0 00-2 2v12a2 2 0 002 2h8a2 2 0 002-2V7.414A2 2 0 0015.414 6L12 2.586A2 2 0 0010.586 2H6zm5 6a1 1 0 10-2 0v3.586l-1.293-1.293a1 1 0 10-1.414 1.414l3 3a1 1 0 001.414 0l3-3a1 1 0 00-1.414-1.414L11 11.586V8z" clip-rule="evenodd"></path></svg>
                            </x-button>
                        </a>
                        <a href="{{ route('dailies') }}">
                            <x-cancel-button>
                                {{ __('Cancel ') }}
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
                            </x-cancel-button>
                        </a>
                    </div>
                </x-slot>
            </x-slot>
        </x-form-section>
    </div>


</div>

--- Route ---

Route::get('/reports/byform}',[ExportsController::class,'perameterdaily'])->name('report.parameters');

--- Controller ---

 public function perameterdaily($doneby,$branch,$status,$adatefrom,$adateto,$cdateform,$cdateto)
    {
        $now = date('m_d_Y');
        $filename = "FullOstoDaily_";
        return Excel::download(new dailyparameterexport($doneby,$branch,$status,$adatefrom,$adateto,$cdateform,$cdateto), $filename.$now."xlsx");
    }

--- Class export to Excel ---


<?php

namespace App\Exports;

use App\Models\Daily;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use PhpParser\Node\Stmt\Return_;

class dailyparameterexport implements FromQuery
{
    use Exportable;
    /**
    * @return \Illuminate\Database\Query\Builder
    */


    private $doneby,$branch,$status,$adatefrom,$adateto,$cdateform,$cdateto;

    public function __construct($doneby,$branch,$status,$adatefrom,$adateto,$cdateform,$cdateto)
    {
       $this->doneby = $doneby;
       $this->branch = $branch;
       $this->status = $status;
       $this->adatefrom = $adatefrom;
       $this->adateto = $adateto;
       $this->cdateform = $cdateform;
       $this->cdateto = $cdateto; 
    }

    public function query()
    {
        if ($this->doneby =="" && $this->branch =="" && $this->status =="" && $this->adatefrom =="" && $this->adateto=="" && $this->cdateform=="" && $this->cdateto=="") {
            return Daily::all('id','done_by','branch','project_name','assigned_date','completed_by','takeoff_time','status','total_hours','average_time','days_left','project_cost','notes')
            ->sortByDesc('id')
            ;
        } else {
            return Daily::query()->Orwhere('done_by',$this->doneby)
            ->orWhere('branch',$this->branch)
            ->orWhere('status',$this->status)
            ->where('assigned_date',$this->adatefrom)
            ->where('assigned_date',$this->adateto)
            ->orWhere('completed_by', $this->cdateform)
            ->orWhere('completed_by',$this->cdateto)
            ;
        }
        
    }

    public function headings():array
    {
        return [
            'Namber','Done By','branch','Project Name','Assigned Date','Completed By','TakeOff Time','Status','Total Hours','Average Time','Days Left','Project Cost','Notes'
        ];
    }
     
}
0 likes
1 reply
tykus's avatar

With that parameter list, you're lacking for a DTO (in my opinion)!

What is the reason for using a Livewire component and a regular route and Controller action here?

Please or to participate in this conversation.