Laravel:非法字符串偏移量'answer'

时间:2019-08-08 17:48:03

标签: laravel error-handling

我想使用控制器将数据存储在数据库中,但出现错误

我尝试过:

$newValue = array('answer' => $value); // 
  错误:参数1传递给   Illuminate \ Database \ Grammar :: parameterize()必须为数组类型,   给定的整数

我尝试过:['answer' => $value]

  

错误:相同的错误

我尝试过:

$value['answer']
  

错误:ErrorException(E_WARNING)非法的字符串偏移量'answer'

但数据已保存在数据库中,但将我重定向到此错误页面

控制器:

public function store(Request $request, Survey $survey) 
  {
    // remove the token 
    $arr = $request->except('_token');
    foreach ($arr as $key => $value) {
      $newAnswer = new Answer();      
      $newValue = $value['answer'];
      $newAnswer->answer = $newValue;
      $newAnswer->question_id = $key;
      $newAnswer->user_id = Auth::id();
      $newAnswer->survey_id = $survey->id;
      $newAnswer->commentaire = request()->get('commentaire');
      $newAnswer->last_ip = request()->ip();
      $user = Auth::user();
      givePoint(new VoteAdded($newAnswer, $user));
      $newAnswer->save();

      $answerArray[] = $newAnswer;
    };
    return redirect()->action('SurveyController@view_survey_answers', [$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" type="text" name="{{ $question->id }}[answer]">
                    <label for="answer">Answer</label>
                  </div> 
                @elseif($question->question_type === 'textarea')
                  <div class="input-field col s12">
                    <textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
                    <label for="textarea1">Textarea</label>
                  </div>
                @elseif($question->question_type === 'radio')
                  @foreach($question->option_name as $key=>$value)
                    <p style="margin:0px; padding:0px;">
                      @if($value === 'else')
                        <input name="{{ $question->id }}[answer]" type="radio" id="{{ $value }}" value="answer"/>
                        <label for="{{ $value }}">{{ $value }}</label>
                        <div id="textboxes" style="display: none">
                            <input id="commentaire" type="text" name="{{ $question->id }}[answer]">
                        </div>
                        @else
                        <input name="answer" type="radio" id="{{ $value }}" value="{{ $value}}"/>
                        <label for="{{ $value }}">{{ $value }}</label>
                        @endif
                    </p>
                  @endforeach

0 个答案:

没有答案