How to format an array to pass it to a datatable? server-side.

How to format an array to pass it to a datatable? server-side.

hernandohernando Posts: 10Questions: 3Answers: 0

Link to test case:
http://live.datatables.net/voraroci/3/watch?html,js,output

Description of problem:
I am working datatable in laravel.

When I use ajax. for my datatable. the controller, processes a sql query, and returns it to me without problem.
my datatable, it paints the information in the table fine.
Example on controller.

$data= DetallePedido::where('id_pedido', $idpedido)->get();
return Datatables($data)->make(true);

when it is the result of a single query, and it returns it, everything shows me without problems. everything ok.

but I need information from different tables.
some that have no reference, and I can't join in tables of bd.

$data1= DetallePedido::where('id_pedido', $idpedido)->get();
$data2= User::where('id', $iduser)->get();
$data3= Other::where('id', $id)->firts();
return Datatables($data)->make(true);

So I made different queries, and according to the data of each query, I built an array.

$data = array();
foreach ($data1 as $d) {

        $product= product::where('id', $d->id_p)->first();

        $data[] = array(
            'cant' => $d->id_pedido,
            'id_producto' => $product->id_producto,
            'referencia' =>  $data3->['ref'],
            'nombre' => $product->descrip_producto,
            'emba' => $product->precenta_producto,
            'valor' => $d->valor_producto,
        );
    }

return Datatables($data)->make(true);

and I tried to pass it to my datatable, but it does not understand the data, what could I do, to pass the data correctly?

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Can you post the data you're sending to DataTables, and the column definitions, please. Also the HTML for the table definition. Those three need to be aligned, so the problem is likely to be there.

    Colin

This discussion has been closed.