0
I have an API in Laravel, with a method show(id)
that returns a response->json($array)
. Within that function I have a foreach
, my problem is in response
that does not await the foreach
end to then return json. This is causing a lack of data in the view.
The Laravel is asynchronous?
My function
of the API this way
use ...
Class ProcessesController ()
{
public function show($id){
$db = DB::connection('pgsql');
//$id = nrprocesso
$process = $db->table('processo')->where('nrprocesso',$id)
->join('viatransporte', 'viatransporte.idviatransporte', '=', 'processo.idviatransporte')
->select('processo.*','viatransporte.nmviatransporte')
->get();
//Esse processo não existe
if (!$process) {
return response()-> json([
'message' => 'Record not found process' // Esse processo não existe
], 404);
}
$idprocesso = $process[0]->idprocesso;
$idusuario = $process[0]->idusuario;
$idpessoacliente = $process[0]->idpessoacliente;
$events = $db->table('followupprocesso')->where('followupprocesso.idprocesso',$idprocesso)
->join('eventos', 'eventos.idevento', '=', 'followupprocesso.idevento')
->select('eventos.idevento', 'eventos.nmevento', 'followupprocesso.dtprevisao', 'followupprocesso.dtrealizacao', 'followupprocesso.dtprevisaoinicial', 'followupprocesso.observacao')
->get();
$dicapa = $db->table('dicapa')->where('idprocesso',$idprocesso)
->join('pessoa', 'pessoa.idpessoa', '=', 'dicapa.idpessoaexportadorrepasse')
->select('dicapa.dtregistrodi', 'dicapa.nrdeclaracaoimportacao', 'pessoa.nmpessoa')
->get();
$user = $db->table('usuario')->select('nmusuario')->where('idusuario',$idusuario)->get();
$client = $db->table('pessoa')->select('nmpessoa')->where('idpessoa',$idpessoacliente)->get();
//Alterando key do array events para o nº do evento
foreach($events as $key => $value) {
$new_key = $value->idevento;
unset($events[$key]);
$events[$new_key] = $value;
}
// for($i = 0; $i < count($events); $i ++ ){
// $value = $events[$i];
// $new_key = $events[$i]->idevento;
// unset($events[$i]);
// $events[$new_key] = $value;
// }
//Retornando resultado das consultas
$processget = [
"remotedb" => [
"process" => $process[0],
"events" => $events,
"contarray" => count($events),
"di" => $dicapa,
"user" => $user,
"client" => $client,
],
"localdb" => []
];
return response()->json($processget);
}
}
Sponse is not waiting for the foreach
finish the loop and then return the JSON.
Can you put the code snippet, or an example of how your code is? so we can help more easily
– Antonio
I changed the question with the code
– tenorio
You can add the full code and examples of what the return should be like and how it is?
– Woss
No ... there is something wrong with your code that maybe is bringing wrong information, please put the class in full!
– novic
Maybe the problem is with events. One thing is that Json is asynchronous, another thing is the event. PHP does not usually have this asynchronous no, the problem is something else.
– Wallace Maxters
I think the problem is inside is, but without seeing anyone can know.
– Miguel
return response()->json($get)
, the parentheses in theresponse()
. And if you’ve only put a summary of your code, put it all in. If no one will know exactly what’s in your code.– Diego Souza
I put the real code guys...
– tenorio
I put all the code, you can help me?
– tenorio