Return query via ajax laravel

1

Good evening, could someone please help me on this: I made a select and gave a "get ()" at the end to get the results, I passed them in an array that I'm getting in a function via ajax:

$table = DB::table('empenho as emp')
                ->join('gestora', 'emp.unidGestId', '=', 'gestora.id')
                ->join('gestora_tipo', 'gestora.tipoId', '=', 'gestora_tipo.id')
                ->join('despesa', 'emp.fichaOrcId', '=', 'despesa.id')
                ->join('pessoa', 'emp.fornecedorId', '=', 'pessoa.id')
                ->join('sub_elemento', 'emp.subElementoId', '=', 'sub_elemento.id')
                ->join('fonte', 'emp.fonteId', '=', 'fonte.id')
                ->join('siops', 'emp.siopsId', '=', 'siops.id')
                ->join('cbo', 'emp.cboId', '=', 'cbo.id')
                ->select('gestora_tipo.nome as a', 'emp.nrEmpenho as b', 'emp.date as c', 'emp.hora_insc as d',
                         'emp.valor as e', 'pessoa.nome as f', 'sub_elemento.codSubElem as g', 'despesa.valor as h',
                         'fonte.descricao as i', 'emp.historico as j', 'emp.tipo as k', 'siops.descricao as l',
                         'cbo.descricao as m', 'emp.folha as n')->get();

    $header = ['Gestora', 'Número', 'Data', 'Hora', 'Valor', 'Credor', 'SubElemento',
        'Despesa', 'Fonte', 'Historico', 'Tipo', 'Siops', 'CBO', 'Folha'];

    return ['Tipo' => 'Empenho', 'header' => $header, 'table' => $table, 'id' => $id];

However, I am not able to show the result containing the fields filled with database values, when I do a for ajax it takes up the variable $ header, but does not get the $ table, I can not do this return work, could someone tell me what I'm wrong?

function retornaEmpenho(json){
var result= $('<div>');
for(var i = 0; i < json.header.length; i++)
{   
    var divInner = $('<div>');
    divInner.addClass(json.header[i]);
    divInner.append(json.header[i]+': '+json.table[i]);
    result.append(divInner);
}

When I do this, I throw in the fields with the values, the header works (of course), but the $ table does not return the right value, just an "undefined" value, somebody help me please! Thank you in advance.

    
asked by anonymous 30.03.2016 / 06:32

1 answer

0

[RESOLVED]

One of the fields would not let me return, and even then when it started to return I would return an object, then doing a mapping of the object transforming into an array will solve everything. Thanks!

for(var i = 0; i < json.header.length; i++)
{
    for(var j = 0; j < json.table.length; j++)
    {
    var arr = $.map(json.table[j], function(value){
        return [value];
    });
}
    var divInner = $('<div>');
    divInner.addClass(json.header[i]);
    divInner.append(json.header[i]+': ');
    divInner.append(arr[i]);
    result.append(divInner);
    console.log(arr[i]);
}
    
30.03.2016 / 21:30