Reputation: 5854
I have
Migration file:
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's store
method:
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's the part of wifi
checkbox:
<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>
When I trying creating a record, I'm getting error:
1) I'm trying to save a value as 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) And when I checked Wi-Fi checkbox (TRUE value)
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))
UPDATE:
SOLUTION:
Update from
$booking->wifi = request('wifi');
to
$booking->wifi = request()->has('wifi');
Upvotes: 0
Views: 3426
Reputation: 153
You can check with the has method, if the value has been submitted and therefore you can insert true or false.
You can read it up here in the documenation: https://laravel.com/docs/6.x/requests#retrieving-input
So it has to be something like $request->has('wifi')
. The has method will return true, if the value is present in the request. If this value is true, you can assign true to the field, otherwise false. This would be my approach.
If you need an example, you can look at this question Passing a boolean value from checkbox in Laravel 5.8 form
Upvotes: 2