2

I have a page that allows the user to pick a staff member and a date range(from and to). They can then filter records based on that. I want to have the option to export the data to an Excel format but when I click export, it downloads a blank spreadsheet. Can anyone please help me find my problem here.

Controller:

public function exportvehicles(Request $request)
{
    $startDate = $request->startDate;
    $endDate = $request->endDate;


    return Excel::download(new ExportV($startDate, $endDate), 'excelname.xlsx');
}

Model:

class ExportV implements FromQuery
{
    /**
    * @return \Illuminate\Support\Collection
    */
   
    use Exportable;

    protected $startDate;
    protected $endDate;

    function __construct($startDate,$endDate) {
        $this->startDate = $startDate;
        $this->endDate = $endDate;
    }

    public function query()
    {
        $startDate = request()->input('startDate', '2021-01-01');
        $endDate = request()->input('endDate', '2021-12-12');
        $data = VehicleLog::join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')
        ->join('smsstaff', 'vehicleslog.smsstaff_key', '=', 'smsstaff.smsstaff_key')
        ->when(request()->input('smsstaff_key'), function ($query) {
            $query->where('smsstaff.smsstaff_key', request()->input('smsstaff_key'));
        })
        ->whereDate('log_dt', '>=', $startDate)
        ->whereDate('log_dt', '<=', $endDate)
        ->get();

        return $data;
    }

}

View:

   <label>Pick a staff member</label>
    <select name="smsstaff_key" id="smsstaff_key">
        <option>----------------Select-----------------</option>
        @foreach ($staff as $staffMember) 
           <option value="{{$staffMember->smsstaff_key}}" {{request()->input('smsstaff_key') === $staffMember->smsstaff_key ? 'selected="selected"' : ''}}>{{$staffMember->name}}</option>
        @endforeach
   </select>
   <button  class="btn btn-primary">Filter by selected staff member</button>
  
</div>
<div style="margin-left: 100px;">
    <label>From:</label>
    <input type="date" class="form-control" name="startDate" value="{{ $startDate }}">
</div>
<div style="margin-left: 150px;">
    <label>To:</label>
    <input style="width: 14em" type="date" class="form-control" name="endDate" value="{{ $endDate }}">
</div>
<a href="{{"/users/export/"}}" target="_blank" class="btn btn-primary">Export to Excel</a>

Route:

Route::get('users/export/', 'App\Http\Controllers\Admin\ReportController@exportvehicles');
7
  • if you do a toSql on your query, do you get results from your database? Commented Oct 26, 2021 at 12:35
  • @Thomas Okay I removed the get() and it exports but it exports all the records. Any way to get it to export just the filtered ones? Commented Oct 26, 2021 at 12:46
  • Try to submit your input as form. Filtering is not working because you are submitting the inputs into application Commented Oct 26, 2021 at 12:51
  • @ManojKiranAppathurai How would I do that? Commented Oct 26, 2021 at 12:52
  • can you post the route code for the export operation ? Commented Oct 26, 2021 at 12:55

1 Answer 1

1

When you need to send the values to server to need to submit that as form.

//web.php
Route::post('users/export/', 'App\Http\Controllers\Admin\ReportController@exportvehicles')->name('exportVehiclesToExcel');

View:

<form action="{{route('exportVehiclesToExcel')}}" method="POST">
    <label>Pick a staff member</label>
    <select name="smsstaff_key" id="smsstaff_key">
        <option>----------------Select-----------------</option>
        @foreach ($staff as $staffMember)
        <option value="{{$staffMember->smsstaff_key}}" {{request()->input('smsstaff_key') === $staffMember->smsstaff_key ? 'selected="selected"' : ''}}>{{$staffMember->name}}</option>
        @endforeach
    </select>
    <button class="btn btn-primary">Filter by selected staff member</button>

    </div>
    <div style="margin-left: 100px;">
        <label>From:</label>
        <input type="date" class="form-control" name="startDate" value="{{ $startDate }}">
    </div>
    <div style="margin-left: 150px;">
        <label>To:</label>
        <input style="width: 14em" type="date" class="form-control" name="endDate" value="{{ $endDate }}">
    </div>
    <button type="submit">Export to Excel</button>
</form>

Controller:

public function exportvehicles(Request $request)
{

$startDate = $request->startDate;
$endDate = $request->endDate;
$smsStaffKey = $request->smsstaff_key


return Excel::download(new ExportV($startDate, $endDate, $smsStaffKey), 'excelname.xlsx');
}

Excel Export Class

class ExportV implements FromQuery
{

    use Exportable;

    public $startDate;
    public $endDate;
    public $smsStaffKey;

    function __construct($startDate, $endDate, $smsStaffKey = null)
    {
        $this->startDate = $startDate;
        $this->endDate = $endDate;
        $this->smsStaffKey = $smsStaffKey;
    }

    public function query()
    {
        $startDate = $this->startDate;
        $endDate = $this->endDate;
        $data = VehicleLog::join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')
        ->join('smsstaff', 'vehicleslog.smsstaff_key', '=', 'smsstaff.smsstaff_key')
        ->when($this->smsStaffKey, function ($query) {
            $query->where('smsstaff.smsstaff_key', $this->smsStaffKey);
        })
            ->whereDate('log_dt', '>=', $startDate)
            ->whereDate('log_dt', '<=', $endDate);

        return $data;
    }
}

Comment if there is any issues

Sign up to request clarification or add additional context in comments.

2 Comments

it says The POST method is not supported for this route. Supported methods: GET, HEAD.. Any idea why?
updated my code in routes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.