mercredi 26 février 2020

How to save a boolean value in Laravel 6?

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 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))




Aucun commentaire:

Enregistrer un commentaire