将Json String保存到数据库Laravel中

时间:2018-04-11 17:56:58

标签: json database laravel

我无法理解如何将arra作为JSON字符串保存到我的数据库中。我已经尝试了一些我看过的教程,但似乎没有一个很好的解决方案。我希望答案一起保存到一个表行中。当我输入代码时,它显示了arrey中的所有asnwers,但我不能保存它:

  

数组到字符串转换(SQL:插入answersuser_id,   template_idanswer)值(3,1,   AOJf3lEibJCqqbdGH2ha86EKgvgP1QrijQmQ8Btp))“

这是我的代码。商店控制器:

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

查看:

                      @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>   

我的数据库条目:

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

我也不确定我的表中的答案是否应该设置为字符串。 任何帮助,将不胜感激!谢谢。

2 个答案:

答案 0 :(得分:0)

由于$answers->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');
}

第二种:你可以将$request->answer加密为字符串..所以数组将是一个字符串..但是当你从数据库中检索数据时,你必须解密它..

第三种:您可以使用数组内爆将数组转换为字符串

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 :(得分:0)

$ answers-&gt; answer = json_encode($ request-&gt; all())