【问题标题】:Save a Json String into the database Laravel将 Json 字符串保存到数据库 Laravel
【发布时间】:2018-09-21 17:26:37
【问题描述】:

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

数组到字符串的转换(SQL:insert into answers(user_id, template_id, answer) 值 (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');
        });
    }

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

【问题讨论】:

标签: json database laravel


【解决方案1】:

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

【讨论】:

  • 哇,它做到了!非常感谢,不知道我错过了这个。
【解决方案2】:

你的错误是因为$answers-&gt;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-&gt;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');
}

【讨论】:

  • 尽管上述是暂时的完美答案。我想试试你提到的那些。为了能够使用 user_id 和 template_id 检索它并在视图中显示它们中的每一个,哪种方法是最简单甚至最好的方法。我不是在寻找任何复杂的东西。这是一个 uni 项目,所以我不介意更简单的出路。
猜你喜欢
  • 2012-07-04
  • 2018-03-04
  • 1970-01-01
  • 2016-12-13
  • 2023-03-18
  • 2021-02-28
  • 1970-01-01
  • 2016-04-03
  • 2014-08-21
相关资源
最近更新 更多