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