Is Laravel’s JSON return asynchronous?

Asked

Viewed 387 times

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.

  • 2

    Can you put the code snippet, or an example of how your code is? so we can help more easily

  • I changed the question with the code

  • You can add the full code and examples of what the return should be like and how it is?

  • 1

    No ... there is something wrong with your code that maybe is bringing wrong information, please put the class in full!

  • 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.

  • I think the problem is inside is, but without seeing anyone can know.

  • return response()->json($get), the parentheses in the response(). 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.

  • I put the real code guys...

  • I put all the code, you can help me?

Show 4 more comments

2 answers

0

Laravel’s sponse is not asynchronous, see if there is no exit point within the foreach that is causing this.

  • I identified the problem in,

0


I identified the problem in

foreach($events as $key => $value) {

   $new_key = $value->idevento;  
   unset($events[$key]);     
   $events_2[$new_key] = $value;
}

create a new array and add the new values to it, then delete that $Events array.

Browser other questions tagged

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