0

I want to store data in my database using controller but i got errors

I tried :

$newValue = array('answer' => $value); // 

error : Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given,

I tried : ['answer' => $value]

error : the same error

I tried :

$value['answer']

error : ErrorException (E_WARNING) Illegal string offset 'answer'

but the data is saved in the database but it's redirect me to this erro page

Controller :

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]);
  }

view :

 @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
Salem loress
  • 359
  • 1
  • 5
  • 13
  • The error means that $value is a string not an array so you cannot do this: `$value['answer'];` – dparoli Aug 08 '19 at 18:14
  • how to fix it ? – Salem loress Aug 08 '19 at 18:15
  • You have to inspect $value content, try with `dd($value);` – dparoli Aug 08 '19 at 18:18
  • SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'commentaire' for column 'question_id' at row 1 (HY000) SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'commentaire' for column 'question_id' at row 1 (HY000) – Salem loress Aug 08 '19 at 18:23
  • SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'answer' cannot be null (23000) SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'answer' cannot be null (23000) i fixed all but i got this even the data is saved in database , please how to fix that error ? – Salem loress Aug 08 '19 at 18:45
  • Possible duplicate of [Illegal string offset Warning PHP](https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php) – dparoli Aug 08 '19 at 19:37
  • `name="{{ $question->id }}[answer]"` <- this is wrong. – JTinkers Aug 08 '19 at 20:25
  • i fixed but the problem still up – Salem loress Aug 10 '19 at 22:35

0 Answers0