I need to retrieve data from two tables in the database, registrations and ssi_tracks. The goal is to display data from the registrations table based on the track_first_status field. I also require filtering of the data by date range and presenting it in a datatable.
Below is a sample of the controller file with a custom function:
public function datewise(Request $request, $data_end, $data_start){
$data_start->$request->input('start_date');
$data_end->$request->input('end_date');
$datewise=DB::table('registrations')
->join('ssi_tracks', 'registrations.registration_id', '=', 'ssi_tracks.registration_id')
->select('address', 'model', 'chassis', 'delivery_date')
->where([["ssi_tracks.track_first_status", "=", 0]])
->whereBetween('created_at',[$data_start, $data_end])
->get();
$output = "";
$count = 1;
foreach ($datewise as $reg) {
$output .= '<tr>' .
'<td>' . $count++ . '</td>' .
'<td>' . $reg->address . '</td>' .
'<td>' . $reg->model . '</td>' .
'<td>' . $reg->chassis . '</td>' .
'<td>' . $reg->delivery_date . '</td>' .
'<td>' . '<button class="btn btn-primary btn-rounded button">Call Customer
</button>'. '</td>' .
'</tr>';
}
return response()->json($output);
}
Here is an example of the Ajax Function:
<script>
$(function () {
$("#start_date").datepicker();
$("#end_date").datepicker();
});
$('#filter').click(function () {
let start_date = $('#start_date').val();
let end_date = $('#end_date').val();
if (start_date !== '' && end_date !== '') {
$.ajax({
url: "{{url("date")}}",
method: "get",
data: {
start_date: start_date,
end_date: end_date
},
success: function (data) {
// $('#listdetails').html(data);
console.log(data);
}
});
} else {
alert("Please Select Date");
}
});
});
</script>
The index File includes input fields for selecting dates:
<label>From
</label> <input type="date" name="start_date" id="start_date" class="form-control"
style="width:150px;">
<label>To</label> <input type="date" name="end_date" id="end_date" class="form-control"
style="width:150px;">
<button class="btn btn-info" id="filter" name="filter" >Filter</button>