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