【问题标题】:How to insert array field in Laravel 5.6如何在 Laravel 5.6 中插入数组字段
【发布时间】:2018-12-20 07:01:48
【问题描述】:

如何使用 Laravel 插入多个输入无线电数据?

当我选择一个单选按钮并单击提交表单以插入我的数据库时

这是我的html:

<table class="table table-bordered  table-condensed table-hover">
  <thead>
    <tr>
      <th bgcolor="#DFF0D8">
        <h5>
          <b>
            <center>Question No</center>
          </b>
        </h5>
      </th>
      <th bgcolor="#DFF0D8">
        <h5>
          <b>
            <center>Yes</center>
          </b>
        </h5>
      </th>
      <th bgcolor="#DFF0D8">
        <h5>
          <b>
            <center>No</center>
          </b>
        </h5>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td id="question1">
        <h5>1.You Like It's
        </h5>
      </td>
      <!-- true checkbox Q1 -->
      <td>
        <div class="flat-green single-row">
          <div class="radio ">
            <center>
              <input name="q1[]" type="radio" value="1">
            </center>
          </div>
        </div>
      </td>
      <td>
        <div class="flat-green single-row">
          <div class="radio ">
            <center>
              <input name="q1[]" type="radio" value="0">
            </center>
          </div>
        </div>
      </td>
    </tr>
    <tr>
      <td id="question2">
        <h5>2.you don't like it's
        </h5>
      </td>
      <!-- true checkbox Q2 -->
      <td>
        <div class="flat-green single-row">
          <div class="radio ">
            <center>
              <input name="q2[]" type="radio" value="1">
            </center>
          </div>
        </div>
      </td>
      <td>
        <div class="flat-green single-row">
          <div class="radio ">
            <center>
              <input name="q2[]" type="radio" value="0">
            </center>
          </div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

我的 QuestionController.php :

$question = new Question;
$question ->id = $request->get(id);
$question ->question_no = 'question1';
$question ->score = $request->get('q1');
$question ->save();
return redirect('/questionForm/create');

但它只能向数据库插入一行,我想像这样向数据库表插入数据:

|id|user_id|question_no|score|
|1 |  1    | question1 |  1  |
|2 |  1    | question2 |  0  |

如何向数据库表中插入多条数据?

【问题讨论】:

    标签: php eloquent laravel-5.6


    【解决方案1】:

    多个数据意味着您必须插入多次。 $request-&gt;get('q1')$request-&gt;get('q2') 都生成数组,因此您必须遍历它们(或 array_sum(),或将 q1/2 值聚合到单个 score 列所需的任何操作)。

    现在,假设 score 是检查值的总和,id 是一个自动增量列,它看起来像这样:

        // deal with the q1 array
        $q1_array = $request->input('q1');
        $question = new Question;
        foreach ($q1_array as $q1_answer) {
            // i'm assuming table.score is the sum of the values of the arrays
            $q1_total =+ $q1_answer;
        }
        $question->question_no = 'question1';
        $question->score = $q1_total;
        $question->save();
    
        // now deal with the q2 array
        $q2_array = $request->input('q2');
        $question = new Question;
        foreach ($q2_array as $q2_answer) {
            // i'm assuming table.score is the sum of the values of the arrays
            $q2_total =+ $q2_answer;
        }
        $question->question_no = 'question2';
        $question->score = $q2_total;
        $question->save();
    

    当然,这只是为了清楚起见。您可以在外面有一个单独的循环,这样您就不会重复 question1 和 question2 代码。

    这是一个更缩写的版本,假设您的表名为answers

        DB::table('answers')->insert([
            ['user_id' => $userId, 'question_no' => 'question1', 'score' => array_sum($request->input('q1'))],
            ['user_id' => $userId, 'question_no' => 'question2', 'score' => array_sum($request->input('q2'))],
        ]);
    

    【讨论】:

    • 如何获取标签 td 中的文本 id 你有解决方案吗?
    • 哦,&lt;td&gt; 标签中的数据在 POST 过程中不会传递,所以你必须将它们放入 &lt;input type="hidden"&gt; 标签中。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签