yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 857 forks source link

Draw always returns 0 #3142

Closed Visal-Sok closed 6 months ago

Visal-Sok commented 6 months ago

Summary of problem or feature request

Hi, I'm not sure what the problem is, but for the past few days, I've been stuck on this problem, and i can't seem to find the source of the issue. I am quite new to your package, and it would be nice if you can let me know what the problem I am having is.

The "draw" attribute seems to always return a 0, but the data returns as expected

{
  "draw": 0,
  "recordsTotal": 50,
  "recordsFiltered": 50,
  "data": [...],
  "disableOrdering": false,
  "queries": [
    {
      "query": "select count(*) as aggregate from \"quotations\"",
      "bindings": [],
      "time": 0.1
    },
    {
      "query": "select * from \"quotations\"",
      "bindings": [],
      "time": 0.07
    }
  ],
  "input": []
}

I am implementing my project in the microservices architecture, thus testing on a laravel project that will later be implemented to a lumen project, but the snippet provided is all done in Laravel 11.x, both the frontend and the backend.

I'm not sure if this helps, but this is the get URL generated from the javascript, which did specify the "draw" param as not 0

http://127.0.0.1:8500/quotation/ajax?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&order%5B0%5D%5Bname%5D=&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1715136289315

Code snippet of problem

This is my endpoint:

Route::get('/fetch', function () {
    $quotation = Quotation::query()->select('*');
    return DataTables::of($quotation)->make(true);
});

and my receiving frontend

$(document).ready(function () {
    let table = $('#myTable').DataTable({
        search: true,
        stateSave: true,
        processing: true,
        searching: true,
        responsive: true,
        type: "POST",
        serverSide: true,
        ajax: {
            url: '{!! route('ajax-fetch-quotation') !!}',
        }
    });
});

even with the addIndexColumn() function, doesn't seem to help

Route::get('/fetch', function () {
    $quotation = Quotation::query()->select('*');
    return DataTables::of($quotation)->addIndexColumn()->make(true);
});

Could it be something in my config/datatables.php (It is the published default), a mismatched between the GET request and the required params, or maybe it is something else. I would very much aprpreciate any help!

System details

yajra commented 6 months ago

Based on debug logs, "input": [] is empty. This is usually due to server configuration issue on URL encoding.

yajra commented 6 months ago

See https://github.com/yajra/laravel-datatables/issues/1210

Visal-Sok commented 6 months ago

You pointed me at the right direction! It turns out i didn't include the query in my gateway connector, hence the empty input. I didn't know what to look for and you point me to the right direction. Thank you!