Query scanning all records

Asked

Viewed 62 times

0

Good morning guys, could someone help me with this comeback? Well, I got this select:

public function Empenho($id)
{
    $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('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',
                     'cbo.descricao as l', 'emp.folha as m')->get();

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

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

The get method takes all the information, however I am passing an "id" parameter that receives the identification of each database record, so I do the mapping and the return of select:

function retornaEmpenho(json){
var result= $('<div>');

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);
}       
    return result;
}

Everything is working normally, however I only managed to map the entire record, so my variable "arr" will always receive the last record, because it was the last value assigned, what I wanted to know is, how do I for my variable "arr" mapping only the "$id" and return me only the "$id" fields, does anyone know how I do it? I’m using the Rover. Grateful from now on !

1 answer

0


After the line:

$table = DB::table('empenho as emp')

insert your 'Where' like this:

->where('nome_da_coluna_de_id', $id)

Try this 'Where' there and replace 'Join' ('by 'leftJoin'('.

  • I thank the reply friend, however did not solve, I tried in the same way that you told me, I also tried so: ->Where('commitment', 'commitment.id', '=', $id), however both did not solve. Thanks anyway, I keep trying.

  • What returned the query with this where ? can you explain better @Lincolnbinda? What was the return?

  • Try this 'Where' there and replace 'Join(' with 'leftJoin' (' in all occurrences.

  • Thanks for the answers, I have to wait for my reputation to reach 15 so I can be reputed, I’m a little short, I’m totally new in the forum and still can’t signal how useful, however I especially thank my friend Jhonnyjks for helping me solve the problem. Grateful !

  • Quiet! I arrived yesterday in this PT version of the stack overflow, we will contribute and strengthen!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.