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

goncalo-diaas's avatar

Session Manipulation

I'm developing a questionnaire with several pages and different types of questions. When I tried to save the progress of the questionnaire in session, I found that every time the page reloads, the saved information disappears. I welcome ideas for solving this problem and improvements to the code I'm sending.

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\SurveyQuestion;
use App\Models\Question;
use Illuminate\Support\Facades\Session;
use Illuminate\Database\Eloquent\Collection;

class SurveyForm extends Component
{
    public $currentPage = 1;
    public $formData = [];
    public Collection $surveyQuestions;
    public $survey;

    public function mount($currentPage, $survey)
    {
        $this->currentPage = $currentPage;
        $this->surveyQuestions = $this->getQuestions($survey, $currentPage);
        $this->survey = $survey;
    }

    public function nextPage()
    {
        Session::push($this->survey->id, $this->formData);
        $this->currentPage++;
        $this->surveyQuestions = $this->getQuestions($this->survey, $this->currentPage);
        dd(Session::get($this->survey->id));
    }

    public function previousPage()
    {
        $this->currentPage--;
        $this->surveyQuestions = $this->getQuestions($this->survey, $this->currentPage);

    }

    public function submitForm()
    {

    }

    function getQuestions($survey, $currentPage) {
        $surveyQuestions = SurveyQuestion::where('survey_id', $survey->id)->first();
        $questionIds = json_decode($surveyQuestions->questions);
        $questionIds = $questionIds[$currentPage - 1];

        return Question::whereIn('id', $questionIds)->get();
    }
}
?>

0 likes
0 replies

Please or to participate in this conversation.