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

Walker's avatar

Foreach in the view over the data in the session

Hi I'm trying to foreach in the view over the data in the session. I get a foreach () argument argument must be of type array | object, string given. Thank you

if (!$request->session()->exists('formData')) {
            $request->session()->put('formData', ['Data', $request->all()]);
        } else {
            $request->session()->push('formData', ['Data', $request->all()]);
        }

public function index()
    {
        $product = Product::get();

        $data = array(session('formData'));

        return view('cart.index', [
            'product' => $product,
            'data' => $data
        ]);
    }
@foreach ($data as $value)
                            {{ $value->consignee_lastName }}
                            @endforeach

0 likes
5 replies
Tray2's avatar

Why are you storing form data in the session?

Walker's avatar

This is data that does not need to be stored in the db

rajeev2pd's avatar

Could you print this - $data = array(session('formData'));

Please or to participate in this conversation.