The modal below contains 2 datepickers From
and To
:
<div class="form-group row">
<div class="col-12 col-md-6">
<label for="from_date" class="mb-2 mb-md-0 col-12 col-md-12 text-muted">From</label>
<div class="col-md-12">
<input
id="from"
type="date"
name="from"
class="form-control"
value-type="YYYY-MM-DD"
format="YYYY-MM-DD"
:input-attr="{name: 'from'}"
ref= "from"
>
</div>
</div>
<div class="col-12 col-md-6">
<label for="to_date" class="mb-2 mb-md-0 col-12 col-md-12 text-muted">To</label>
<div class="col-md-12">
<input
id="to"
type="date"
name="to"
class="form-control"
v-model.trim="$v.export.to.$model"
value-type="YYYY-MM-DD"
format="YYYY-MM-DD"
:input-attr="{name: 'to'}"
ref= "to"
>
</div>
</div>
<div>
<button type="button" v-on:click="submitQuery()">Export</button>
</div>
</div>
The button will call submitQuery()
function:
submitQuery() {
var url = "{{ route('timesheet.export') }}?";
var table = $(this.$el)
.find("#table-list")
.DataTable();
var params = $.param(table.ajax.params());
window.location.href = url + params;
}
web.php
Route::get('/report/export', 'TimesheetController@export')->name('timesheet.export');
The export()
function in controller:
public function export(Request $request, TimesheetFilters $filters){
$from_date=$request->from;
$to_date = $request->to;
$query = DB::table('timesheets')
->whereBetween('created_at',[$from_date,$to_date])
->orderBy('id');
$data = generateDatatables($query, $filters, $request, 'timesheet');
return Excel::download(new TimesheetExport($data), 'Report.xlsx');
}
TimesheetExport
:
<?php
namespace AppExports;
use AppTimesheet;
use IlluminateContractsViewView;
use MaatwebsiteExcelConcernsFromView;
use MaatwebsiteExcelConcernsFromQuery;
use MaatwebsiteExcelConcernsExportable;
use MaatwebsiteExcelConcernsWithHeadings;
use MaatwebsiteExcelConcernsFromCollection;
use MaatwebsiteExcelConcernsShouldAutoSize;
use IlluminateSupportFacadesDB;
class AllTimesheetExport implements FromView, ShouldAutoSize {
use Exportable;
protected $date;
public function __construct($data) {
$this->data = $data;
}
public function view(): View {
$data = $this->data;
return view('excel.timesheet', compact('data'));
}
}
Lastly, I have this blade template for the export structure:
<table class="table table-bordered table-hover table-striped table-sm">
<thead>
</thead>
</table>
<table class="table table-bordered table-hover table-striped table-sm">
<thead>
<tr>
<th>ID</th>
<th>Timesheet No</th>
<th>Status</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($data as $key => $timesheet)
<tr>
<td>{{ $timesheet->id }}</td>
<td>{{ $timesheet->number }}</td>
<td>{{ $timesheet->status_code }}</td>
<td>{{ CarbonCarbon::parse($timesheet->created_at)->format('d/m/Y') }}</td>
</tr>
@endforeach
</tbody>
</table>
The export procedure an empty excel file. How to fix this?