0

I am having trouble understanding how to save an arra as a JSON string into my database. I have tried a few tutorials I seen but none seemed to be a good solution. I want the answers to save into one table row together. When I dd my code it shows all of the asnwers in an arrey, but I cant save it as I get:

Array to string conversion (SQL: insert into answers (user_id, template_id, answer) values (3, 1, AOJf3lEibJCqqbdGH2ha86EKgvgP1QrijQmQ8Btp))"

Here is my code. Controller for store:

public function store(Request $request, Template $template, Question $question, Answer $answer)
{
    $answers = new Answer;
    $answers->user_id = auth()->user()->id; //currently logged in user
    $answers->template_id = $template->id;  //currently use template
    $answers->answer = $request->all();
    // dd($answers);

    $answers->save();
    return back()->with('success', 'Your Answers were Successfully Created into a note');
}

View:

                      @foreach ($questions as $question) <!-- Index counts the questions shown -->

                {!! Form::open(['action' => ['AnswersController@store', $template->id], 'method' => 'POST']) !!}     
                      <div class="panel panel-default">
                          <div class="panel-body">
                              <p class="pull-left question2"> {{$question->question}}</p>
                              <div class="form-group answer">

                            {{Form::label('', '')}}
                            {{Form::text('answer[]', null, array('class'=>'form-control', 'placeholder' => 'Type in your answer'))}}
                            {{-- {{Form::hidden('question[]',  $question->question, null, array('class'=>'form-control'))}}  --}}
                                </div>
                          </div>
                        </div>

                    @endforeach
                    {{Form::submit('Save', ['class'=>'btn btn-primary'])}}
                    {!! Form::close() !!} 
                    <hr>   

My database entry:

  public function up()
    {
        Schema::create('answers', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('templates')->onDelete('cascade');
            $table->integer('template_id')->unsigned()->nullable();
            $table->foreign('template_id')->references('id')->on('templates')->onDelete('cascade');
            $table->string('answer');
        });
    }

I am also not sure if the answer in my table should be set as a string. Any help would be appreciated! Thank you.

1

2 Answers 2

2

$answers->answer = json_encode($request->all())

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow that did it! Thanks so much no idea I was missing this.
0

Your error is occurs because of $answers->answer.

Your answer data type is string. If you want to store answer into your DB.. You have so many ways..

The first: You loop the answer

public function store(Request $request, Template $template, Question $
question, Answer $answer)
{
    foreach($request->answer as $ans) {
      $answers = new Answer;
      $answers->user_id = auth()->user()->id; //currently logged in user
      $answers->template_id = $template->id;  //currently use template
      $answers->answer = $ans;
    // dd($answers);
      $answers->save();
    }
    return back()->with('success', 'Your Answers were Successfully Created into a note');
}

The second: you can encrypt your $request->answer as string.. so the array will be one string.. but when you retrieve the data from DB, you have to decrypt it..

The third: you can use array implode which make the array into a string

public function store(Request $request, Template $template, Question 
$question, Answer $answer)
{
    $answers = new Answer;
    $answers->user_id = auth()->user()->id; //currently logged in user
    $answers->template_id = $template->id;  //currently use template
    $answers->answer = implode(", ", $request->answer);
    // dd($answers);

    $answers->save();
    return back()->with('success', 'Your Answers were Successfully Created into a note');
}

1 Comment

Even though the above was the perfect asnwer for the time being. I want to try the ones you mentioned. For being able to retrieve this with a user_id and template_id and show each one of these in view, which method is the easiest or even best to do. I am not looking for anything complicated. This is a uni project, so I don't mind an easier way out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.