Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MonsieurRado's avatar

Add a checkbox field to user (laravel8)

Hello,

I would like to add a "sport" field for users. I was able to change the register page and make it work. A user can check one or more sports when registering. I added a column for each option in the user table. ("football", "rugby", and "tennis"). If a sport is checks, the value in the database is 1, if not it's 0.

This is ok.

Then, on the profile page, i didn't succeed to make the update profile information work. Profile informations are good, but if i change the "sports" and save, it doesn't save it.

Here is my code:

The form in update-profile-information-form.blade.php :

    <div class="col-span-6 sm:col-span-4">
        <div class="field">
            <label class="label is-small">Sports:</label>
            <div class="control">
              <label class="checkbox">
                <input type="checkbox" name="football" value="1" {{ Auth::user()->football === '1' ? 'checked' :''}} wire:model.defer="state.football">
                    Football
                </label>
                <label class="checkbox">
                <input type="checkbox" name="rugby" value="1"  {{ Auth::user()->rugby === '1' ? 'checked' :''}}>
                    Rugby
                </label>
                <label class="checkbox">
                <input type="checkbox" name="tennis" value="1"  {{ Auth::user()->tennis === '1' ? 'checked' :''}}>
                    Radio
                </label>                   
            </div>
        </div>
        
    </div>

And this is the code in UpdateUserProfileInformation.php:

public function update($user, array $input){ Validator::make($input, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)], 'photo' => ['nullable', 'image', 'max:1024'], 'football' => ['numeric'], 'rugby' => ['numeric'], 'tennis' => ['numeric'], ])->validateWithBag('updateProfileInformation');

    if (isset($input['photo'])) {
        $user->updateProfilePhoto($input['photo']);
    }

    if(!isset($input['football'])){
        $input['football']=0;
    }
    if(!isset($input['rugby'])){
        $input['rugby']=0;
    }
    if(!isset($input['tennis'])){
        $input['tennis']=0;
    }

    $user->forceFill([
        'name' => $input['name'],
        'email' => $input['email'],
        'football' => $input['football'], 
        'rugby' => $input['rugby'],
        'tennis' => $input['tennis'],
    ])->save();
}

If someone could help me because I'm stucked.

Thank you :)

0 likes
10 replies
MichalOravec's avatar

If is not checked the value is not sended to the backend, so you need to check if the key is in array,

$user->update([
    'name' => $input['name'],
    'email' => $input['email'],
    'football' => array_key_exists('football', $input) ? 1 : 0, 
    'rugby' => array_key_exists('rugby', $input) ? 1 : 0, 
    'tennis' => array_key_exists('tennis', $input) ? 1 : 0
]);

Docs: https://www.php.net/manual/en/function.array-key-exists.php

MonsieurRado's avatar

Thank you for your answer MichalOravec.

I tried but it doesnt work.

jlrdw's avatar

Try

$yourcheck = !empty($request->yourcheck) ? 1 : 0;

Replace yourcheck with your values.

I normally use the request facade, so

$yourcheck = !empty(Request::input('yourcheck')) ? 1 : 0;
MonsieurRado's avatar

Thank you jlrdw,

Here is the code:

$football = !empty($request->football) ? 1 : 0; $rugby = !empty($request->rugby) ? 1 : 0; $tennis = !empty($request->tennis) ? 1 : 0;

    $user->forceFill([
        'name' => $input['name'],
        'email' => $input['email'],
        'football' => $football, 
        'rugby' => $rugby, 
        'tennis' => $tennis,
    ])->save();

It still doesnt work, nothing changes in the database. I don't know why.

I tried to do the same think with an input type="text" field and it works. I don't understand why changes arent'saved.

jlrdw's avatar

Try

//Top of controller put

use Illuminate\Support\Facades\DB;



            $postdata = array(
                        'name' => $input['name'],
                        'email' => $input['email'],
                        'football' => $football, 
                        'rugby' => $rugby, 
                        'tennis' => $tennis,
            );

            DB::table('users')->insert($postdata);

Make sure your fields are correct type, boolean is tinyint for 1 or 0.

Edit: But for users table, how about password.

newbie360's avatar

can you put the code inside

```

your code

```

MichalOravec's avatar

Why you don't add it into $fillable then update() will work.

MonsieurRado's avatar

Jlrdw, the password is handled by another file: updateuserpassword.php

i tried this:

use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
use Illuminate\Support\Facades\DB;


class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
    /**
     * Validate and update the given user's profile information.
     *
     * @param  mixed  $user
     * @param  array  $input
     * @return void
     */
    public function update($user, array $input)
    {
        

        $football = !empty($request->football) ? 1 : 0;
        $rugby = !empty($request->rugby) ? 1 : 0;
        $tennis = !empty($request->tennis) ? 1 : 0;

        $postdata = array(
                        'name' => $input['name'],
                        'email' => $input['email'],
                        'football' => $football, 
                        'rugby' => $rugby, 
                        'tennis' => $tennis,
                        'password' => 'test',
            );

            DB::table('users')->insert($postdata);
    }
}

this added a new user in the table but the fields "football", "rugby", "tennis" are always 0, 0, 0

So there is an issue with the input and so the form i think...

thank you Newbie360

MichalOravec, here is the User model, with the fillable variables:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'football',
        'rugby',
        'tennis',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];
}

jlrdw's avatar

You probably need to use a combination of echo, dd, and the network tab to check your response and see what's going on.

Please or to participate in this conversation.