With this code, I can search the data with the date range and it shows in the view file with the total count number. Now, I want to export the data with the date range using Maatwebsite/Laravel-Excel package (from query) or any other good method.
Can anyone please help me with this?
Blade View
<div class="panel-heading">
<div class="row">
<div class="col-md-5">Total Records - <b><span id="total_records"></span></b></div>
<div class="col-md-5">
<div class="input-group input-daterange">
<input type="text" name="from_date" id="from_date" readonly class="form-control" />
<div class="input-group-addon">to</div>
<input type="text" name="to_date" id="to_date" readonly class="form-control" />
</div>
</div>
<div class="col-md-2">
<button type="button" name="filter" id="filter" class="btn btn-info btn-sm">Filter</button>
<button type="button" name="refresh" id="refresh" class="btn btn-warning btn-sm">Refresh</button>
</div>
</div>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th width="6%">Owner Manager</th>
<th width="6%">CSR Name</th>
<th width="6%">CSR EID</th>
</tr>
</thead>
</table>
JQuery Code
$(document).ready(function () {
var date = new Date();
$('.input-daterange').datepicker({
todayBtn: 'linked',
format: 'yyyy-mm-dd',
autoclose: true
});
var _token = $('input[name="_token"]').val();
fetch_data();
function fetch_data(from_date = '', to_date = '') {
$.ajax({
url: "{{ route('dr.fetch_data') }}",
method: "POST",
data: {
from_date: from_date,
to_date: to_date,
_token: _token
},
dataType: "json",
success: function (data) {
var output = '';
$('#total_records').text(data.length);
for (var count = 0; count < data.length; count++) {
output += '<tr>';
output += '<td>' + data[count].owner_manager + '</td>';
output += '<td>' + data[count].csr_name + '</td>';
output += '<td>' + data[count].csr_eid + '</td>';
}
$('tbody').html(output);
}
})
}
$('#filter').click(function () {
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if (from_date != '' && to_date != '') {
fetch_data(from_date, to_date);
} else {
alert('Both Date is required');
}
});
$('#refresh').click(function () {
$('#from_date').val('');
$('#to_date').val('');
fetch_data();
});
});
Controller Code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class DRController extends Controller
{
function index()
{
return view('dr');
}
function fetch_data(Request $request)
{
if ($request->ajax()) {
if ($request->from_date != '' && $request->to_date != '') {
$data = DB::table('users_info')->whereBetween('ptdate', array($request->from_date, $request->to_date))->get();
} else {
$data = DB::table('users_info')->orderBy('ptdate', 'desc')->get();
}
echo json_encode($data);
}
}
}
With this code, I can search the data with the date range and it shows in the view file with the total count number. Now, I want to export the data with the date range using Maatwebsite/Laravel-Excel package (from query) or any other good method.
Can anyone please help me with this?
Blade View
<div class="panel-heading">
<div class="row">
<div class="col-md-5">Total Records - <b><span id="total_records"></span></b></div>
<div class="col-md-5">
<div class="input-group input-daterange">
<input type="text" name="from_date" id="from_date" readonly class="form-control" />
<div class="input-group-addon">to</div>
<input type="text" name="to_date" id="to_date" readonly class="form-control" />
</div>
</div>
<div class="col-md-2">
<button type="button" name="filter" id="filter" class="btn btn-info btn-sm">Filter</button>
<button type="button" name="refresh" id="refresh" class="btn btn-warning btn-sm">Refresh</button>
</div>
</div>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th width="6%">Owner Manager</th>
<th width="6%">CSR Name</th>
<th width="6%">CSR EID</th>
</tr>
</thead>
</table>
JQuery Code
$(document).ready(function () {
var date = new Date();
$('.input-daterange').datepicker({
todayBtn: 'linked',
format: 'yyyy-mm-dd',
autoclose: true
});
var _token = $('input[name="_token"]').val();
fetch_data();
function fetch_data(from_date = '', to_date = '') {
$.ajax({
url: "{{ route('dr.fetch_data') }}",
method: "POST",
data: {
from_date: from_date,
to_date: to_date,
_token: _token
},
dataType: "json",
success: function (data) {
var output = '';
$('#total_records').text(data.length);
for (var count = 0; count < data.length; count++) {
output += '<tr>';
output += '<td>' + data[count].owner_manager + '</td>';
output += '<td>' + data[count].csr_name + '</td>';
output += '<td>' + data[count].csr_eid + '</td>';
}
$('tbody').html(output);
}
})
}
$('#filter').click(function () {
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if (from_date != '' && to_date != '') {
fetch_data(from_date, to_date);
} else {
alert('Both Date is required');
}
});
$('#refresh').click(function () {
$('#from_date').val('');
$('#to_date').val('');
fetch_data();
});
});
Controller Code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class DRController extends Controller
{
function index()
{
return view('dr');
}
function fetch_data(Request $request)
{
if ($request->ajax()) {
if ($request->from_date != '' && $request->to_date != '') {
$data = DB::table('users_info')->whereBetween('ptdate', array($request->from_date, $request->to_date))->get();
} else {
$data = DB::table('users_info')->orderBy('ptdate', 'desc')->get();
}
echo json_encode($data);
}
}
}
you can use this Laravel Excel package its very helpful
https://docs.laravel-excel./3.1/exports/
you can istall it using this ande :
poser require maatwebsite/excel
create excel export class using :
php artisan make:export UsersExport --model=User
In UsersExport class add this :
<?php
namespace App\Exports;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
class PostsExport implements FromQuery, WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
use Exportable;
protected $from_date;
protected $to_date;
function __construct($from_date,$to_date) {
$this->from_date = $from_date;
$this->to_date = $to_date;
}
public function query()
{
$data = DB::table('users_info')
->whereBetween('pdate',[ $this->from_date,$this->to_date])
->select('ownermanager','crsname','crsaid')
->orderBy('id');
return $data;
}
public function headings(): array
{
return [
'Owner Manager',
'CSR Name',
'CSR Aid',
];
}
}
In your controller :
public function export(Request $request)
{
$from_date=$request->from_date;
$to_date = $request->to_date;
return Excel::download(new usersExport($from_date,$to_date), 'excelname.xlsx');
}
don't forget to add :
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
Finally make a route to this function :
Route::get('userinfo/export/', 'DRController@export')->name('drc.export');
now if we call this url :
http://127.0.0.1:8000/userinfo/export?from_date=2020-07-01&to_date=2020-07-20
it will download an excel file like you have in your view.