Laravel - loading data into an array when performing loop querys

Asked

Viewed 46 times

-1

I’m performing querys in a loop on a controller and need to go loading the information in a array to then send to a view and print.

Turns out I don’t know how to input the data as follows

[id_motorista]-> $motoristas[$c]['id'],
[nome]->$motoristas[$c]['nome'],
[valor total de corridas]-> $relatorio
)

And I’m still having to use the pointer in the array as select sent me a multidimencional array the following select

$motoristas = Motorista::all()->sortBy('nome');

// então eu conto o total de motoristas para ser o laço
 $num_mot = Motorista::all()->count();
// aqui declaro o array vazio para ser carregado no laço
$mot_total = array();

//Segue o script, as datas vem de um formulário anterior e estão funcionando corretamente

for($c = 0; $c < $num_mot; $c++){
            $relatorio = Corrida::all()
            ->whereBeTween('momentosolicitacao',["$datas_ini","$datas_fin"])
            ->where('status','Finalizada')
            ->where('motoristas_id',$motoristas[$c]['id'])
            ->sum('valorcorrida'); 
            $id_motorista = (id,motoristas);
            $id_mot = $motoristas[$c]['id'];
            $dadomotorista = $motoristas[$c]['nome'];
            $mot = array($dadomotorista => $relatorio);
            array_push($mot_total ,$id_mot, $mot);

1 answer

0

The solution was as follows:

After each query I loaded the Array this way

 $mot_cham = array(
     'motorista_id' => $motoristas[$c]['id'],
     'motorista_nome' => $motoristas[$c]['nome'],
     'V_total_chamados' => $relatorio,
     'T_chamados' => $chamados,
 );
 array_push($mot_total, $mot_cham);

What returned me a multidimensional array to be treated in view

Browser other questions tagged

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