Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ThyagoSB's avatar

Display session data in the view.

Good afternoon. I'm trying to create a cart like this.

public function adicionar(Request $request, $id){

$qtd = $request->input('qtd'); $produtos = DB::select('select nome from produtos where id = ?',[$id]); session()->push('sessao_carrinho',$produtos,$qtd); return view('lista_pedidos');

}

And I present it in my view like this ...

@foreach(Session::get('sessao_carrinho') as $test) {{ $test->produtos }} {{ $test->qtd }} @endforeach

But it is a mistake.

Trying to get property of non-object

Is that right the way I'm doing the cart and presenting it in the view?

0 likes
2 replies
Cronix's avatar

Why put in session just to read from session? Just pass the data to the view, like normal.

public function adicionar(Request $request, $id){

    $qtd = $request->input('qtd');
    $produtos = DB::select('select nome from produtos where id = ?',[$id]);

    return view('lista_pedidos', compact('produtos'));    
}

in view

@foreach ($produtos as $product)
    <div>{{ $product->nome }}</div>
@endforeach 
ThyagoSB's avatar

THEREFORE YOU WILL ONLY SEE THE LAST REGISTRATION I ADDED TO THE CART, I NEED THE PRODUCT AND QUANTITY TO BE SAVED IN SESSION, SO NOT TO LOSE THE ABOVE.

Please or to participate in this conversation.