0

I have the following form, this is a simple form with only a single radio button:

@extends('layouts.app')

@section('content')
        <h1>Accomodation</h1>
        <p>This is the accomodation page.</p>

    {!! Form::open(['action' => ['UserController@update', Auth::id()], 'method' => 'POST']) !!}
            <div class="form-group">
                    {{Form::label('ConsiderForAccommodation', 'Consider Me For Accommodation')}}
                    {{Form::checkbox('ConsiderForAccommodation', 'Consider Me For Accommodation', false)}}
            </div>
            {!! Form::token() !!}
            {{Form::hidden('_method', 'PUT')}}
            {{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
    {!! Form::close() !!}
@endsection

I am then trying to update the value "ConsiderForAccomodation" which is a field I added to the users table:

 /**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function update(Request $request)
{
    // Update the given user
    $user = auth()->user();

    $user->ConsiderForAccommodation = $request->input('ConsiderForAccommodation');

    $user->save();

    return redirect('/accommodation');
}

However, upon checking the box and then submitting the form, I am given this error:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Consider Me For Accommodation' for column 'ConsiderForAccommodation' at row 1 (SQL: update `users` set `updated_at` = 2019-03-18 20:44:37, `ConsiderForAccommodation` = Consider Me For Accommodation where `id` = 1)

It seems to be reaching this update method, however, I seem to be doing something wrong as it's not picking up the value of the checkbox.

What am I doing wrong? If more of the code is required I'll be happy to update the post.

EDIT - User table migration, looking in MySQL the column has been created as a "TinyInt" which as far as I know is the correct type:

class UserAddAccomodationFields extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    // Add field "ConsiderForAccommodation" to table.
    Schema::table('users', function(Blueprint $table)
    {
        $table->boolean('ConsiderForAccommodation');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    // Remove field "ConsiderForAccommodation" from table.
    Schema::table('users', function(Blueprint $table)
    {
        $table->dropColumn('ConsiderForAccommodation');
    });
}
}
5
  • Could you add your migration(s) for the table? Commented Mar 18, 2019 at 20:52
  • I'm not sure what the signature for Form::checkbox is, but it looks like it's setting the value of $request->input("ConsiderForAccommodation") to Consider me for Accommodation Commented Mar 18, 2019 at 20:52
  • @rpm192 Added :) Commented Mar 18, 2019 at 21:02
  • @TimLewis - Aye, I noticed that too, however looking through my example projects and such, the implemented way should be bringing across the value of the checkbox not the label. Commented Mar 18, 2019 at 21:03
  • Should yes, but quite clearly isn't, as $request->input("ConsiderForAccommodation"); shows that it contains literally that string. Check your source code; (right click your checkbox and inspect element), you'll probably see <input type="checkbox" name="ConsiderForAccommodation" value="Consider Me For Accommodation"/> (or similar). Commented Mar 18, 2019 at 21:05

2 Answers 2

1

In the laravelcollective/html documentation I see the following.

Form::checkbox('name', 'value', true);

Which means that you should change your checkbox code to this:

{{Form::checkbox('ConsiderForAccommodation', 1, false)}}

I'm assuming that the value gets passed in the request when the checkbox is ticked and that it returns null otherwise, not entirely sure though.

Assuming the above, you can do something like this in your controller:

if($request->input('ConsiderForAccomodation') == null) {
   $user->ConsiderForAccomodation = 0;
} else {
   $user->ConsiderForAccomodation = 1;
   // or $user->ConsiderForAccomodation = $request->input('ConsiderForAccomodation');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Was just about to post that I got it working by testing the returned value for NULL and also recoding the checkbox element on the view. Thank you for posting this :)
No problem! Glad to help.
0

I think you have error in your Sql Table Structure.

`SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Consider Me For Accommodation' for column 'ConsiderForAccommodation' at row 1 (SQL: update `users` set `updated_at` = 2019-03-18 20:44:37, `ConsiderForAccommodation` = Consider Me For Accommodation where `id` = 1)

`

This Means That ConsiderForAccommodation Column is Datetime Format. You are trying to Write an Integer in it.

Please Look at Migrations Or Sql Table And Update it if you want to use it as boolean or Integer

2 Comments

I've included the migration for the user table in my post, the field is being created as a boolean (in MySQL it's also appearing as a TinyInt which should be correct).
Check your checkbox value in source Then check value of it in request. dd($request->all())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.