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

emilpapelas4@gmail.com's avatar

Single question/ page.

Hello i have work on a Survey project and on view survey page where survey load i want to show only 1 Question. Once user choose an answer, they will allow to proceed to next Question. Currently on page will show all questions that current survey have.

public function viewsurvey(Survey $survey)
    {
        $survey->option_name;
        return view('offerwall.view', compact('survey'));
    }
<div class="mx-0 mx-sm-auto">
    <div class="card">
      <div class="card-header bg-primary">
        <h5 class="card-title text-white mt-2" id="exampleModalLabel">{{ $survey->title }}</h5>
      </div>
      <div class="modal-body">
        <div class="text-center">
          <i class="far fa-file-alt fa-4x mb-3 text-primary"></i>
          <p>
            <strong>Your opinion matters</strong>
          </p>
          <p>
            {{ $survey->description }}?
            <strong>Give us your feedback.</strong>
          </p>
        </div>
  
        <hr />
  
        
        {!! Form::open(array('action'=>array('App\Http\Controllers\AnswerController@store', $survey->id))) !!}
        @forelse ($survey->questions as $key=>$question)
          <p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
              @if($question->question_type === 'text')
                <div class="input-field col s12">
                  <input id="answer" class="block p-2.5 mb-2 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:outline-none" type="text" name="{{ $question->id }}[answer]">
                </div>
              @elseif($question->question_type === 'textarea')
                <div class="input-field col s12">
                  <textarea id="textarea1" class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded border border-gray-300 placeholder-gray-400 focus:outline-none" name="{{ $question->id }}[answer]"></textarea>
                </div>
              @elseif($question->question_type === 'radio')
                @foreach((array)$question->option_name as $key=>$value)
                  <p style="margin:0px; padding:0px;">
                    <input name="{{ $question->id }}[answer]" class="w-4 h-4 text-blue-600 bg-gray-100 focus:outline-none" type="radio" id="{{ $key }}" />
                    <label for="{{ $key }}">{{ $value }}</label>
                  </p>
                @endforeach
              @elseif($question->question_type === 'checkbox')
                @foreach((array)$question->option_name as $key=>$value)
                <p style="margin:0px; padding:0px;">
                  <input type="checkbox" id="something{{ $key }}" class="w-4 h-4 text-blue-600 bg-gray-100 focus:outline-none" name="{{ $question->id }}[answer]" />
                  <label for="something{{$key}}">{{ $value }}</label>
                </p>
                @endforeach
              @endif 
            <div class="divider" style="margin:10px 10px;"></div>
        @empty
          <span class='flow-text center-align'>Nothing to show</span>
        @endforelse
      {{ Form::submit('Submit Survey', array('class'=>'btn waves-effect waves-light')) }}
      {!! Form::close() !!}
      
      </div>
      <div class="card-footer text-end">
        <button type="button" class="btn btn-primary">Submit</button>
      </div>
    </div>
  </div>
0 likes
9 replies
vincent15000's avatar

What is the aim of this line $survey->option_name; ? It does nothing.

vincent15000's avatar

@emilpapelas4@gmail.com I said that this line does nothing.

public function viewsurvey(Survey $survey)
{
    $survey->option_name; // <= THIS LINE DOES NOTHING
    return view('offerwall.view', compact('survey'));
}
emilpapelas4@gmail.com's avatar

Anyone know what i need to do to make my code showing 1 question / page?

1 like
vincent15000's avatar

@emilpapelas4@gmail.com For example in your code your have a foreach loop to loop over the questions. If you need to display only one question, there are several solutions to do that.

Either you load all questions and then you handle them with JavaScript to display one at a time. Or you load only one question and then you need to write an appropriated route to display the needed question.

Route::get('survey/{survey}/question/{question}', [SurveyQuestionController::class, 'get']);

In the controller you can check which question you need to display.

public function get(Survey $survey, Question $question)
{
	$next = $question->next(); // assuming you write a ```next()``` method in the model to easily access the next question and next can be null if there isn't any next question
	return view('my_question_view', compact('question', 'next'));
}

And in the view you have just to display the selected question (no @forelse loop anymore) but you have to adapt your code because there isn't any $key.

Well ... I think that it's your job to write the code now ;).

emilpapelas4@gmail.com's avatar

@vincent15000 thanks for your answer , i have done this

Route::get('/offerwall/survey/{survey}/q/{question}/{user}', [ApiController::class, 'viewsurvey']);
 $q = Question::where('survey_id',$sv->id)->where('id',$question->id)->first();
{{$q->title}}

Once i want to get the $q->option_name will get htmlspecialchars(): Argument #1 ($string) must be of type string, array given (View: C:\xampp\htdocs\LaravelSurvey\resources\views\offerwall\view.blade.php)

Please or to participate in this conversation.