如何将表单输入保存到json数据中并保存到1个字段表中

时间:2019-03-26 04:11:10

标签: arrays json laravel

我有这样的单选按钮输入表单

 <form class="form-group" action="/user" method="post">
  <table class="table table-striped">
    <tbody><tr>
      <th style="width: 10px">#</th>
      <th>Pertanyaan</th>
      <th style="width: 60px">Tidak Baik</th>
      <th style="width: 60px">Baik</th>
    </tr>
    <tr>
      <td>1.</td>
      <td><input style="border:none" type="text" name="text1" value="Periksa kondisi kelistrikan dan kabel ,pastikan tidak ada yang terkelupas"></td>

      <td>
          <label><input type="radio"  name="input1" value="false" checked></label>
      </td>
      <td>
          <label><input type="radio"  name="input1" value="true"></label>
      </td>
    </tr>
    <tr>
      <td>2.</td>
      <td><input style="border:none" type="text" name="text2" value="Periksa kondisi kabel dan tempat sambungan"></td>

          <td>
              <label><input type="radio"  name="input2"  value="false" checked></label>
          </td>
          <td>
              <label><input type="radio"  name="input2" value="true"></label>
          </td>
    </tr>
    <tr>
      <td>3.</td>
      <td><input style="border:none" type="text" name="text3" value="Periksa kondisi pencetakan (tinta dan kertas printer)"> </td>
      <td>
          <label><input type="radio"  name="input3"  value="false"  checked></label>
      </td>
      <td>
          <label><input type="radio"  name="input3" value="true"></label>
      </td>
    </tr>
    <tr>
      <td>4.</td>
      <td> <input style="border:none" type="text" name="text4" value="Fix and squish bugs"> </td>
      <td>
          <label><input type="radio"  name="input4"  value="false" checked></label>
      </td>
      <td>
          <label><input type="radio"  name="input4" value="true"></label>
      </td>
    </tr>

    <input type="hidden" name="alat_id" value="1">        
    <input type="hidden" name="_token" value="{{ csrf_token() }}">

  </tbody>

</table>
      <div class="box-footer">
        <button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
       </div>
</form>

从此输入中,我得到了结果(偶然出错)

环境和详细信息:

GET Data empty
POST Data
question1   
"Periksa kondisi kelistrikan dan kabel ,pastikan tidak ada yang terkelupas"
answer1 
"false"
question2   
"Periksa kondisi kabel dan tempat sambungan"
answer2 
"false"
question3   
"Periksa kondisi pencetakan (tinta dan kertas printer)"
answer3 
"false"
question4   
"Fix and squish bugs"
answer4 
"false"
alat_id 
"1"
_token  
"buno7LyZ9PLaq3ByKpDYlNz0TuaagV8HPbhqznT8"
name    
"post"

我想在数据库的“ Pemeliharaan”表的“ Pertanyaan”列中输入此信息。

我想像这样添加此字段表格:

{ "question 1":"true", "question 2":"false", 
  "question 3":"true", "question 4":"false",
  "question 5":"true" }

我想这样保存,因为我想将其转换为json数据。

我的控制器:

public function store(Request $request)
{
    $user = new pemeliharaa;
    $id = Auth::user()->id;

    $user->user_id = $id;
    $user->alat_id = $request->alat_id;
    $user->pertanyaan ="";/in here i dont know what i do
    $user->catatan = "empty";
    $user->status = "week";


    $user->save();
    dd($user);

}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

只需使用json_encode()

/etc/profile
/root/.bash_profile
/home/dolphin/.bashrc
/home/dolphin/.bash_profile
public function store(Request $request)
{
    $user = new pemeliharaa;
    $id = Auth::user()->id;

    $user->user_id = $id;
    $user->alat_id = $request->alat_id;
    $user->pertanyaan = json_encode($request->except(['_token']));
    $user->catatan = "empty";
    $user->status = "week";


    $user->save();
    dd($user);

}