Request Laravel 5

Asked

Viewed 41 times

-2

I need to save the values of the variable $result in a session, follow the code.

class CorreioController extends Controller
{

    public function calculate(Request $request)
    {
     
             
     $params = array(
        'nCdEmpresa'=>NUll,
        'sDsSenha'=>NULL,
        'sCepOrigem'=>14942020,   
        'sCepDestino'=>$_POST['sCepDestino'],
        'nVlPeso' =>$_POST['nVlPeso'],
        'nCdFormato' =>'1',
        'nVlComprimento' =>15,
        'nVlAltura' =>15,
        'nVlLargura' =>15,
        'sCdMaoPropria' => 'n',
        'nVlValorDeclarado' => '0',
        'sCdAvisoRecebimento' => 'n',
        'nCdServico' => $_POST['nCdServico'],
        'nVlDiametro' => '0',            
        'StrRetorno' => 'xml',
        'nIndicaCalculo' => '3'
     );
        $url = ('http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?') . http_build_query($params);
        $c = curl_init($url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        $fact = curl_exec($c);
        $xml = simplexml_load_string($fact);
        $json = json_encode($xml);
        $result = json_decode($json, true);

    }
   
}

Yeah, I’d like to reuse those values in a view.

  • Isn’t it easier for you to just call the view you need with the variable in it? You can do this in the Trolle with: Return view('nameDaView',Compact('variavel1','variavel2','variavelN'));

  • Note: to convert XML to array just do $result = (array) $xml, no need to convert to JSON.

1 answer

-1


You can save values in php’s Session with the function session(['chave' => 'valor ou variavel']); and retrieve them with the session()->get('chave'), but do not get confused with php and view sessoes, the same session q is in php is only php, has nothing to do with the Js sessionStorage function. In your case you could just store everything in a variable and return to view with compact(). For example... Controller:

$variavel = [
                'nome' => 'João',
                'sobrenome' => 'Silva'
            ];

return view('view...')
            ->with(compact('variavel'));

In the Blade:

O nome do usuário é {{ $variavel['nome'] . ' '  . $variavel['sobrenome']}}
  • 1

    Thank you so much I really needed the value saved in the session. Your reply helped me a lot because I didn’t know how to access the stored values

Browser other questions tagged

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