How to validate and insert data of multiple checkboxes in register.blade.php laravel?

Tags: , , , ,



I added 2 checkbox inputs to the register.blade.php file. I want one of them to be required, i.e. the user must select one or the other, otherwise I’d like to display some kind of error message like “These fields cannot be empty”. I tried adding an array called role_id[] on both input fields. I also added value=”1″ on the first input and value=”2″ on the second input, because I want either 1 or 2 to go into my Users Table into the column called role_id, because I’ve created a Roles Table with 2 records, 1 is jobseeker and 2 is employer and obviously there is a relationship here. So basically if the user selects the first input which is “I’m a Job Seeker” which has a value of 1, 1 will go into the role_id column in the Users Table in the database and same idea for checkbox 2 “Im an Employer”. For some reason I’m able to register but nothing is being inserted the database.

RegisterController.php file:

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'role_id' => ['required'],
        ]);
    }

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'role_id' => $data['role_id'],
        ]);
    }

register.blade.php file:

<div class="form-group row">
                                    <div class="col-md-8 offset-md-2 text-md-right">
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="checkbox" name="role_id[]" id="job-seeker" value="1">
                                            <label class="form-check-label" for="job-seeker">I'm a Job Seeker</label>
                                        </div>
                                        <div class="form-check form-check-inline">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input class="form-check-input" type="checkbox" name="role_id[]" id="employer" value="2">
                                            <label class="form-check-label" for="employer">I'm an Employer</label>
                                        </div>
                                    </div>
                                </div>

Answer

Hi User Model doesn’t come with role_id by default. You should edit AppUser.php and on protected $fillable, add 'role_id'.

Try again. This time the value of rule_id should be mass assigned by the Model to the database.

EDIT #1: In the form, use:

<input class="form-check-input" type="checkbox" name="role_id" value="1">
<input class="form-check-input" type="checkbox" name="role_id" value="2">

You dont need array if only 1 value should be passed. And btw, a RADIO input should work even better since only one input can be selected.



Source: stackoverflow