You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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/html is in your composer.json (match the version to your Laravel release).
  • Confirm Collective\Html\HtmlServiceProvider::class is listed in config/app.php under providers.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:28:01