Laravel 5.6问卷应用无法显示问题的答案字段
Hey there, let's figure out why your radio/checkbox options and answer fields aren't showing up in your survey form. From the snippet you shared, it looks like your Blade template gets cut off at the @if($quest... part—this incomplete conditional logic is almost certainly the root cause. Let's walk through the fixes step by step:
1. Complete the Question Type Conditional Rendering
You need to explicitly check each question's type and render the corresponding form control. Here's how to flesh out your template properly, assuming your Question model has a type field (like radio, checkbox, or text) and an options relationship for choice-based questions:
{{$survey->title}} {{$survey->description}} Created by: {{$survey->user->name}} {!! Form::open(array('action'=>array('AnswerController@store',$survey->id)))!!} @forelse($survey->questions as $key=>$question) <div class="mb-4"> <h5>Question {{$key+1}} - {{$question->title}}</h5> {{-- Render radio buttons for single-choice questions --}} @if($question->type === 'radio') @foreach($question->options as $option) <div class="form-check"> {!! Form::radio("answers[{$question->id}]", $option->id, false, ['class' => 'form-check-input']) !!} {!! Form::label("answers[{$question->id}]", $option->text, ['class' => 'form-check-label']) !!} </div> @endforeach {{-- Render checkboxes for multiple-choice questions --}} @elseif($question->type === 'checkbox') @foreach($question->options as $option) <div class="form-check"> {!! Form::checkbox("answers[{$question->id}][]", $option->id, false, ['class' => 'form-check-input']) !!} {!! Form::label("answers[{$question->id}][]", $option->text, ['class' => 'form-check-label']) !!} </div> @endforeach {{-- Render text input for open-ended questions --}} @elseif($question->type === 'text') {!! Form::text("answers[{$question->id}]", null, ['class' => 'form-control', 'placeholder' => 'Type your answer here']) !!} @endif </div> @empty <p>This survey has no questions yet.</p> @endforelse {{-- Add a submit button (easy to forget!) --}} <div class="mt-3"> {!! Form::submit('Submit Your Answers', ['class' => 'btn btn-primary']) !!} </div> {!! Form::close() !!}
2. Verify Your Laravel Collective Form Setup
Since you're using Form::open() and related syntax, make sure you've properly set up the Laravel Collective HTML package:
- Double-check that
laravelcollective/htmlis in yourcomposer.json(match the version to your Laravel release). - Confirm
Collective\Html\HtmlServiceProvider::classis listed inconfig/app.phpunderproviders. - Ensure the aliases are set in
config/app.php:'aliases' => [ // ... 'Form' => Collective\Html\FormFacade::class, 'Html' => Collective\Html\HtmlFacade::class, ]
3. Validate Your Model Relationships
Make sure your survey, question, and option models have correct relationships:
// Survey.php public function questions() { return $this->hasMany(Question::class); } // Question.php public function options() { return $this->hasMany(Option::class); // Only if you have an Option model for choices }
Also, when fetching the survey in your controller, eager-load the questions and their options to avoid N+1 queries:
$survey = Survey::with('questions.options')->findOrFail($surveyId);
4. Debug to Confirm Data is Loaded
If you're still stuck, add temporary debug output to your template to check if data is being passed correctly:
{{-- Check if questions are loaded --}} <pre>{{ print_r($survey->questions->toArray(), true) }}</pre> {{-- Check question type and option count --}} @foreach($survey->questions as $question) <p>Question Type: {{ $question->type }} | Options Count: {{ $question->options->count() }}</p> @endforeach
This will tell you if your questions are missing type values, or if options aren't being loaded at all.
内容的提问来源于stack exchange,提问作者M Saadat

