如何将复选框值保存到数据库 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40355848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:41:10  来源:igfitidea点击:

How to save checkbox value to database Laravel

phplaravel

提问by Andrew Vanusi

here's my form code :

这是我的表单代码:

 <div class="form-group">
          <label>Warranty:</label>
          {{ Form::checkbox('warranty', 0, false) }}
 </div>

here's my controller code :

这是我的控制器代码:

public function save(Request $request, $obj = null) {

if (!$obj) {
    $obj = new Service;
}
(Input::has('warranty')) ? true : false;
return $this->saveHandler($request, $obj);
}

it throws this error Class 'App\Http\Controllers\Input' not found

它抛出这个错误 Class 'App\Http\Controllers\Input' not found

any idea ?

任何的想法 ?

回答by Amit Gupta

You can use requestobject to get the warrantyinput as:

您可以使用requestobject 来获取warranty输入:

$request->get('warranty')

or

或者

$request->has('warranty')

回答by Alexey Mezenin

Inputfacade was removed in latest verison, so you can add it to config/app.php:

Input最新版本中删除了外观,因此您可以将其添加到config/app.php

'Input' => Illuminate\Support\Facades\Input::class,

Or add this line to the beginning of a controller:

或者将此行添加到控制器的开头:

use Illuminate\Support\Facades\Input;

Or write full path when you use it:

或者在使用时写完整路径:

(\Illuminate\Support\Facades\Input::has('warranty')) ? true : false;

回答by Balraj Allam

Add this at top in the controller,

将此添加到控制器的顶部,

use Illuminate\Support\Facades\Input;

回答by Krzysztof Dziuba

 $request->merge(array('checkbox_name' => $request->has('checkbox_name') ? true : false));
Object::create($request->all());

This is how I save boolean parameters which are checkboxes in form.

这就是我保存表单中复选框的布尔参数的方式。

Or simply give your checkbox value

或者只是给你的复选框值

 {{ Form::checkbox('checkbox_name', 1) }}