Monika Zygadlo
Monika Zygadlo

Reputation: 21

Save a Json String into the database Laravel

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.

Upvotes: 0

Views: 7480

Answers (2)

shubiao-yao
shubiao-yao

Reputation: 36

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

Upvotes: 2

Jems
Jems

Reputation: 1706

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

Upvotes: 0

Related Questions