Can I pass 2 Laravel array_encode?

Asked

Viewed 91 times

2

I can pass 2 json_encode in a single function in the Laravel?
What is the best way to return 2 JSON different?
Where 1 consults the table Products and the other the table States.

Look at this controller of mine:

   $data = Produtos::select(
                      DB::raw('nome as nome'),
                      DB::raw('count(*) as number'))
                    ->groupBy('nome')
                    ->get();

    $array[] = ['Nome', 'Number'];

    foreach ($data as $key => $value) {
      $array[++$key] = [$value->nome, $value->number];
    }

    /**
    * CALCULO DOS ESTADOS
    */

    $estados = Estados::selectRaw('SUM(valor) as valor, estado as estado')->groupBy('estado')->get();
    $arrayEstados[] = ['Valor', 'Estado'];
    foreach($estados as $key => $valor) {
        $arrayEstados[++$key] = [$valor->valor, $valor->estado];

    }

    return view('home', compact('notasEntrada', 'notasSaida', 'valorNotasEntrada', 'valorNotasSaida'))->with('nome', json_encode($array), 'estados', json_encode($arrayEstados));

}

Note that in the return view I’m passing 2 json_encode but I am not having result, this is allowed to do? What is the best solution?

1 answer

0


Try to pass the Return in this way:

return view('home', compact('notasEntrada', 'notasSaida', 'valorNotasEntrada', 'valorNotasSaida'))->with('nome', json_encode($array))->with('estados', json_encode($arrayEstados));

It is possible to send 2 with(); or even more, as needed.

Reference: https://laravel.com/docs/5.7/views#Passing-data-to-views

Browser other questions tagged

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