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

Marcolino922's avatar

Retrieve session data in blades

I would like to create a multi step form with validations, but the values are not stored in the inputs through the sessions.

In the controller I have:

use Illuminate\Support\Facades\Session;

My code at the moment is this:

        public function addItemStep1(Request $request)
            {
        
                $product = $request->session()->get('product');
        
                return view('admin.items.add', compact('product'))->with([
                    'page_name' => __('Add Item')
                ]);
        
            }
    
    public function storeNewItemStep1(Request $request)
    {

        $validatedData = $request->validate([
            'title' => 'required',
            'body' => 'required'
        ]);

        if(empty($request->session()->get('product')))
        {
            $product = new Items();
            $product->fill($validatedData);
            $request->session()->put('product', $product);

        } else {
            
            $product = $request->session()->get('product');
            $product->fill($validatedData);
            $request->session()->put('product', $product);

        }
        
        return redirect('/');

    }

The validations work, everything is ok, but unfortunately after sending the form the data just entered does not appear.

<input type="text" class="form-control @error('title') is-invalid @enderror" value="{{ Session::get('title') }}" name="title">
{{ session()->get('product.title') }}
0 likes
2 replies
SilenceBringer's avatar

@marcolino922 here

$product = new Items();

you define products as an object, not as array, so, you are not aable to use dot notation.try like so:

{{ optional(session('product'))->title }}
Marcolino922's avatar

I did it like this:

public function addItemStep1(Request $request)
    {

        $product = new Items();

        return view('admin.items.add', compact('product'))->with([
            'page_name' => __('Add Item')
        ]);

    }
<input type="text" class="form-control @error('title') is-invalid @enderror" value="{{ optional(session('product'))->title }}" name="title">

Unfortunately, however, nothing, it does not recover the data just entered ... with old () instead yes.

Please or to participate in this conversation.