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

reinisk22's avatar

Saving data in a session over several requests

Hello,

I need to create a method that will save data in an array from the first request in a session, and then keep adding data to the array until a certain point.

I got this method but it only saves data from the previous request and isn't adding anything over subsequent requests. Could someone please help out?

    public function show(Request $request): View
    {
        $question = $this->http->getQuestions();

        $answers = [
            $question['number'],
            100,
            200,
            300
        ];

        if ($request->session()->has('asked_questions')) {
            $request->session()->push('asked_questions', $question);
            $request->session()->reflash();
        }

        $request->session()->flash('asked_questions', $question);
        
        return view('question')
            ->with('question', $question['text'])
            ->with('answers', $answers);
    }
0 likes
4 replies
Snapey's avatar

flash is for one request only, not sure why you are using this?

reinisk22's avatar

@Snapey My intention is to reflash. I did manage to figure out how to do it in the end, thanks!

reinisk22's avatar

@webrobert I'm not sure I see the difference. Flash saves data in a session and is available in the next request. If I then reflash the session, it will keep the previously flashed data and pass it on.

I checked the docs and didn't really see a noticeable difference. Could you please tell me a bit more about what you meant? (also please ignore my example, it's not correct.

Please or to participate in this conversation.