0

我试图创建一个调查网站,所以我找到了一些我想对其进行测试并根据需要进行更改的示例,但问题是当我选择问题类型为“收音机”或“复选框”时,我收到此错误消息:

Invalid argument supplied for foreach() (View: /Users/ANA/laravel/app/resources/views/survey/detail.blade.php)

我现在知道传递给 foreach 有问题,但我不知道是什么!这是我第一次使用 Laravel,这就是为什么我很难知道发生了什么。

这是错误消息后突出显示的代码:

  </div>
              </div>
              <?php elseif($question->question_type === 'radio'): ?>
                <?php $__currentLoopData = $question->option_name; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                  <p style="margin:0px; padding:0px;">
                    <input type="radio" id="<?php echo e($key); ?>" />
                    <label for="<?php echo e($key); ?>"><?php echo e($value); ?></label>
                  </p>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
              <?php elseif($question->question_type === 'checkbox'): ?>
                <?php $__currentLoopData = $question->option_name; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <p style="margin:0px; padding:0px;">
                  <input type="checkbox" id="<?php echo e($key); ?>" />
                  <label for="<?php echo e($key); ?>"><?php echo e($value); ?></label>
                </p>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
              <?php endif; ?>
            <?php echo Form::close(); ?>

这是代码detail.blade.php:

    @extends('master')
@section('title', 'Survey')

@section('content')
<div class="card">
    <div class="card-content">
    <span class="card-title"> {{ $survey->title }}</span>
    <p>
      {{ $survey->description }}
    </p>
    <br/>
    <a href='view/{{$survey->id}}'>Take Survey</a> | <a href="{{$survey->id}}/edit">Edit Survey</a> | <a href="/survey/answers/{{$survey->id}}">View Answers</a> <a href="#doDelete" style="float:right;" class="modal-trigger red-text">Delete Survey</a>
    <!-- Modal Structure -->
    <!-- TODO Fix the Delete aspect -->
    <div id="doDelete" class="modal bottom-sheet">
      <div class="modal-content">
        <div class="container">
          <div class="row">
            <h4>Are you sure?</h4>
            <p>Do you wish to delete this survey called "{{ $survey->title }}"?</p>
            <div class="modal-footer">
              <a href="/survey/{{ $survey->id }}/delete" class=" modal-action waves-effect waves-light btn-flat red-text">Yep yep!</a>
              <a class=" modal-action modal-close waves-effect waves-light green white-text btn">No, stop!</a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="divider" style="margin:20px 0px;"></div>
    <p class="flow-text center-align">Questions</p>
    <ul class="collapsible" data-collapsible="expandable">
        @forelse ($survey->questions as $question)
        <li style="box-shadow:none;">
          <div class="collapsible-header">{{ $question->title }} <a href="/question/{{ $question->id }}/edit" style="float:right;">Edit</a></div>
          <div class="collapsible-body">
            <div style="margin:5px; padding:10px;">
                {!! Form::open() !!}
                  @if($question->question_type === 'text')
                    {{ Form::text('title')}}
                  @elseif($question->question_type === 'textarea')
                  <div class="row">
                    <div class="input-field col s12">
                      <textarea id="textarea1" class="materialize-textarea"></textarea>
                      <label for="textarea1">Provide answer</label>
                    </div>
                  </div>
                  @elseif($question->question_type === 'radio')
                    @foreach($question->option_name as $key=>$value)
                      <p style="margin:0px; padding:0px;">
                        <input type="radio" id="{{ $key }}" />
                        <label for="{{ $key }}">{{ $value }}</label>
                      </p>
                    @endforeach
                  @elseif($question->question_type === 'checkbox')
                    @foreach($question->option_name as $key=>$value)
                    <p style="margin:0px; padding:0px;">
                      <input type="checkbox" id="{{ $key }}" />
                      <label for="{{$key}}">{{ $value }}</label>
                    </p>
                    @endforeach
                  @endif
                {!! Form::close() !!}
            </div>
          </div>
        </li>
        @empty
          <span style="padding:10px;">Nothing to show. Add questions below.</span>
        @endforelse
    </ul>
    <h2 class="flow-text">Add Question</h2>
    <form method="POST" action="{{ $survey->id }}/questions" id="boolean">
      <input type="hidden" name="_token" value="{{ csrf_token() }}">
      <div class="row">
        <div class="input-field col s12">
          <select class="browser-default" name="question_type" id="question_type">
            <option value="" disabled selected>Choose your option</option>
            <option value="text">Text</option>
            <option value="textarea">Textarea</option>
            <option value="checkbox">Checkbox</option>
            <option value="radio">Radio Buttons</option>
          </select>
        </div>
        <div class="input-field col s12">
          <input name="title" id="title" type="text">
          <label for="title">Question</label>
        </div>
        <!-- this part will be chewed by script in init.js -->
        <span class="form-g"></span>

        <div class="input-field col s12">
        <button class="btn waves-effect waves-light">Submit</button>
        </div>
      </div>
      </form>
  </div>
</div>
@endsection

你能帮我吗,我在这里苦苦挣扎,我不知道出了什么问题。

4

1 回答 1

0

我发现了问题,除了 master.blade.php 中的这一行之外,一切都是正确的:

{!! MaterializeCSS::include_css() !!}

{!! MaterializeCSS::include_js() !!}

我将它们更改为与另一个相同,并且代码现在可以正常工作^^。只需将它们更改为如下所示:

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
于 2019-02-18T18:34:00.643 回答