@osamashaki read the docs https://datatables.net/examples/data_sources/ajax
Datatable supports ajax source data
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to reload data inside the datatable without refreshing the page, function is returning the data and the data is passed to the datatable but the datatable is not showing the new data. *When I pass the data from a txt file it is working fine.
public function ajaxsalesfilter(Request $request)
{
$bookings = Booking::join('listings', 'bookings.listing_id', '=', 'listings.id')
->get(['bookings.*', 'listings.*', 'bookings.id']);
$bookings = $bookings->filter(function ($b) {
return ($b->is_paid == 1 && !in_array($b->status, [4])) || $b->status == 1;
});
$bookings = $bookings->sortByDesc('booking_date');
$a = array();
foreach($bookings as $b)
{
array_push($a, $b);
}
echo json_encode($a);
}
the js code:
$.ajax({
type:"post",
cache : false,
async: false,
data: form_data,
url: "/admin/ajax/sales/filter",
success:function(data)
{
var obj1 = JSON.parse(JSON.stringify(data));
obj1 = '{ "data": ' + obj1 + '}'; //reformat obj1 to pass to datatable
tbl = $('#example1').DataTable({
ajax: obj1, ///objects2.txt'
columns: [
{ data: 'booking_no'},
{ data: 'booking_date'},
{ data: 'shopping_mall_name'},
{ data: 'lot_no'},
{ data: 'from_date'},
{ data: 'to_date'},
],
"responsive": true,
"autoWidth": false,
"paging": true,
"ordering": true,
"footer": true,
"destroy": true,
"order": []
});
},
error:function(data) {
console.log('error');
}
});
Any clue why? Thank you guys
Please or to participate in this conversation.