如何在 Laravel 6 中保存布尔值?
How to save a boolean value in Laravel 6?
我有
迁移文件:
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]: Integrity
constraint violation: 1048 Column 'wifi' cannot be null (SQL: insert
into bookings
(user_id
, name
, phone
, wifi
, address
,
desc
, updated_at
, created_at
) values (1, Dragon, 12341234, ?,
asdfasdfasdkf hsadfasdf, asdfasdfas, 2020-02-27 07:10:55, 2020-02-27
07:10:55))
2) 当我选中 Wi-Fi 复选框时(真值)
Illuminate\Database\QueryException SQLSTATE[22007]: Invalid datetime
format: 1366 Incorrect integer value: 'on' for column
kaganat
.bookings
.wifi
at row 1 (SQL: insert into bookings
(user_id
, name
, phone
, wifi
, address
, desc
, updated_at
,
created_at
) values (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');
您可以使用 has 方法检查值是否已提交,因此您可以插入 true 或 false。
您可以在此处阅读文档:https://laravel.com/docs/6.x/requests#retrieving-input
所以它必须类似于 $request->has('wifi')
。如果请求中存在该值,则 has 方法将为 return true。如果此值为真,则可以将 true 分配给该字段,否则为 false。这就是我的方法。
需要例子的话可以看看这个问题
我有
迁移文件:
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]: Integrity constraint violation: 1048 Column 'wifi' cannot be null (SQL: insert into
bookings
(user_id
,name
,phone
,wifi
,address
,desc
,updated_at
,created_at
) values (1, Dragon, 12341234, ?, asdfasdfasdkf hsadfasdf, asdfasdfas, 2020-02-27 07:10:55, 2020-02-27 07:10:55))
2) 当我选中 Wi-Fi 复选框时(真值)
Illuminate\Database\QueryException SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'on' for column
kaganat
.bookings
.wifi
at row 1 (SQL: insert intobookings
(user_id
,name
,phone
,wifi
,address
,desc
,updated_at
,created_at
) values (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');
您可以使用 has 方法检查值是否已提交,因此您可以插入 true 或 false。
您可以在此处阅读文档:https://laravel.com/docs/6.x/requests#retrieving-input
所以它必须类似于 $request->has('wifi')
。如果请求中存在该值,则 has 方法将为 return true。如果此值为真,则可以将 true 分配给该字段,否则为 false。这就是我的方法。
需要例子的话可以看看这个问题