I was trying to learn DataTables syntax from techontech's code (https://github.com/techontech/laravel-date-range-filter/blob/main/app/Http/Controllers/StudentController.php) and realized that his method only works for tables that don't use relationships/foreign keys
// Fetch records
function fetch(start_date, end_date) {
$.ajax({
url: "{{ route('students/records') }}",
type: "GEt",
data: {
start_date: start_date,
end_date: end_date
},
dataType: "json",
success: function(data) {
// Datatables
var i = 1;
$('#records').DataTable({
"data": data.students,
// buttons
"dom": "<'row'<'col-sm-12 col-md-4'l><'col-sm-12 col-md-4'B><'col-sm-12 col-md-4'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
"buttons": [
'copy', 'csv', 'excel', 'pdf', 'print'
],
// responsive
"responsive": true,
"columns": [{
"data": "id",
"render": function(data, type, row, meta) {
return i++;
}
},
{
"data": "name"
},
{
"data": "standard",
"render": function(data, type, row, meta) {
return `${row.standard}th Standard`;
}
},
{
"data": "percentage",
"render": function(data, type, row, meta) {
return `${row.percentage}%`;
}
},
{
"data": "result"
},
{
"data": "created_at",
"render": function(data, type, row, meta) {
return moment(row.created_at).format('DD-MM-YYYY');
}
}
]
});
}
});
}
fetch();
// Fetch records
function fetch(start_date, end_date) {
$.ajax({
url: "{{ route('students/records') }}",
type: "GEt",
data: {
start_date: start_date,
end_date: end_date
},
dataType: "json",
success: function(data) {
// Datatables
var i = 1;
$('#records').DataTable({
"data": data.students,
// buttons
"dom": "<'row'<'col-sm-12 col-md-4'l><'col-sm-12 col-md-4'B><'col-sm-12 col-md-4'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
"buttons": [
'copy', 'csv', 'excel', 'pdf', 'print'
],
// responsive
"responsive": true,
"columns": [{
"data": "id",
"render": function(data, type, row, meta) {
return i++;
}
},
{
"data": "name"
},
{
"data": "standard",
"render": function(data, type, row, meta) {
return `${row.standard}th Standard`;
}
},
{
"data": "percentage",
"render": function(data, type, row, meta) {
return `${row.percentage}%`;
}
},
{
"data": "result"
},
{
"data": "created_at",
"render": function(data, type, row, meta) {
return moment(row.created_at).format('DD-MM-YYYY');
}
}
]
});
}
});
}
fetch();
Is there a way to modify this code to accomodate:
- Foreign Keys, for example, instead of
name, we have student_id and would need to retrieve the student's first_name and last_name and put them together to be displayed in the DataTable?