【发布时间】:2020-06-11 03:53:56
【问题描述】:
我有
迁移文件:
public function up()
{
Schema::create('bookings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('name');
$table->string('phone');
$table->boolean('wifi');
$table->string('address');
$table->text('desc');
$table->timestamps();
});
}
BookingController.php的store方法:
public function store()
{
$booking = new Booking();
$booking->user_id = 1;
$booking->name = request('name');
$booking->phone = request('phone');
$booking->wifi = request('wifi');
$booking->address = request('address');
$booking->desc = request('desc');
$booking->save();
return redirect('/bookings');
}
create.blad.php是wifi复选框的一部分:
<div class="field">
<label for="wifi" class="label">Wi-Fi</label>
<div class="control">
<input type="checkbox" class="form-check" name="wifi" id="address">
<div>
</div>
当我尝试创建记录时,出现错误:
1) 我正在尝试将值保存为 FALSE:
Illuminate\Database\QueryException SQLSTATE[23000]:完整性 违反约束:1048 列“wifi”不能为空(SQL:插入 进入
bookings(user_id,name,phone,wifi,address,desc,updated_at,created_at) 值 (1, Dragon, 12341234, ?, asdfasdfasdkf hsadfasdf, asdfasdfas, 2020-02-27 07:10:55, 2020-02-27 07:10:55))
2) 当我选中 Wi-Fi 复选框时(TRUE 值)
Illuminate\Database\QueryException SQLSTATE[22007]:日期时间无效 格式:1366 不正确的整数值:列的“on”
kaganat.bookings.wifi在第 1 行(SQL:插入到bookings(user_id,name,phone,wifi,address,desc,updated_at,created_at) 值 (1, Dragon, 12341234, on, asdfasdfasdkf hsadfasdf, asdfasdfasdfsafaa, 2020-02-27 07:17:15, 2020-02-27 07:17:15))
更新:
解决方案:
更新自
$booking->wifi = request('wifi');
到
$booking->wifi = request()->has('wifi');
【问题讨论】:
-
查看文档:laravel.com/docs/6.x/requests#retrieving-input 您正在从复选框中获取名称值,而不是复选框是否被选中
-
您能否通过展示它应该如何提交一个答案?我会尝试并接受。不幸的是,文档没有给出任何想法如何修复以及在哪里......
标签: php mysql checkbox boolean laravel-6.2