PHP / Laravel-保留复选框值到数据库

时间:2019-02-27 14:49:24

标签: php laravel

请考虑以下更新表单:

<form action="{{ $stream->path() }}" method="POST">
   @csrf
   @method('PATCH')
   <input class="form-control" type="text" name="name" value="">
   <textarea class="form-control" placeholder="Notes..." name="notes" . 
   </textarea>
   <input type="checkbox" name="data_retention">
   <input type="checkbox" name="perform_ocr">    
</form>

每当我发布此表单时,我都会在我的控制器中更新数据库,方法是使用速记并首先验证请求,然后更新数据库。

public function update(Request $request, Stream $stream)
{
        //Update the database.
        $stream->update(request()->validate([
            'name' => 'required|max:255',
            'notes' => 'required|max:255',
            'data_retention' => '',
            'perform_ocr'    => ''
        ]));

        return redirect()->route('streams.show', ['stream' => $stream->token]);
}

现在,每当我提交表单时,都会出现以下错误:

General error: 1366 Incorrect integer value: 'on' for column 'data_retention' at row 1

我在做什么错了?

5 个答案:

答案 0 :(得分:1)

在您的html

<input type="checkbox" name="data_retention" value='1'>
<input type="checkbox" name="perform_ocr" value='1'>

在您的控制器中

$data_retention =  $request->has('data_retention') && $request->get('data_retention') == 1 ? true : false;
$perform_ocr = $request->has('perform_ocr') && $request->get('perform_ocr') == 1 ? true : false;

$stream->update(request()->validate([
    'name' => 'required|max:255',
    'notes' => 'required|max:255',
    'data_retention' => $data_retention,
    'perform_ocr'    => $perform_ocr
]));

答案 1 :(得分:0)

设置复选框的值

<input type="checkbox" name="data_retention" value='1'>
<input type="checkbox" name="perform_ocr" value='1'>   

答案 2 :(得分:0)

您可以内联if更改复选框值。如果未选中此复选框,$data_retention$perform_ocr将是truefalse并将其传递给数据库:

public function update(Request $request, Stream $stream)
 {
    //Update the database.
    $data_retention =  $request->has('data_retention') ? true : false;
    $perform_ocr = $request->has('perform_ocr') ? true : false;

    $stream->update(request()->validate([
        'name' => 'required|max:255',
        'notes' => 'required|max:255',
        'data_retention' => $data_retention,
        'perform_ocr'    => $perform_ocr
    ]));

    return redirect()->route('streams.show', ['stream' => $stream->token]);
  }

答案 3 :(得分:0)

最好与复选框本身分开进行其他操作。

我建议使用JavaScript。

我认为您应该将复选框重命名为其他名称。然后将一个隐藏的输入字段命名为复选框的原始名称。然后,如果选中此复选框,则输入字段的值应为“ off”,但如果选中,则其值为“ on”。

此代码应能解决问题:

<form action="{{ $stream->path() }}" method="POST">
   @csrf
   @method('PATCH')
   <input class="form-control" type="text" name="name" value="">
   <textarea class="form-control" placeholder="Notes..." name="notes" . 
   </textarea>
   <input type="checkbox" name="data_retention_not" id='data1'>
   <input type="checkbox" name="perform_ocr">   
   <input type='hidden'  name='data_retention id='data2'>
   <script>
   if(document.getElementById('data1).checked){
      document.getElementById('data2').value='on';
}    
else
{
    document.getElementById('data2').value='off';
}
</form>

答案 4 :(得分:-1)

我最终得到以下结果:

//Update the database.
$stream['data_retention'] = request()->get('data_retention') ?? 0;
$stream['perform_ocr'] = request()->get('perform_ocr') ?? 0;

$stream->update(request()->validate([
     'name' => 'required|max:255',
     'notes' => 'required|max:255',
]));

return redirect()->route('streams.show', ['stream' => $stream->token]);