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');
get()and it exports but it exports all the records. Any way to get it to export just the filtered ones?