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

Emokores's avatar

How to send multiple checkbox data in Inertia with React to Laravel backend?

Hello guys, I'm trying to send multiple data (user roles) to the backend using InertiaJS with ReactJS through checkboxes. The user roles are dynamically populated from the database. However, when I submit the data, in the dd($request) I don't see the roles being passed. Instead, I see an empty array roles => [].

This is my code in the useForm():


const { data, setData, processing, errors, reset } = useForm({
            fname: "",
            lname: "",
            email: "",
            gender: "",
            mobile: "",
            password: "",
            password_confirmation: "",
            roles: [],
        })

This is the dynamic form element:


{roles.map((role, index) => {
     return (
          <label className="flex items-center" key={index}>
                <Checkbox name="roles[]" id={`role${role.id}`} value={role.id} handleChange={handleChange} required />
                <span className="ml-2 text-sm text-indigo-600">
                          {role.role}
                </span>
          </label>
      );
   }
)}

This is my submit event:


const submit = (e) => {
        e.preventDefault();
        Inertia.post(route("admin.users.store"), data);
};

In PHP, I'm using Fortify actions:

/*
*
* Commented most of the code out for the purpose of testing
*
*/
public function store(Request $request)
{
        // $newUser = new CreateNewUser();

        // $user = $newUser->create($request->only(['name', 'email', 'gender', 'mobile', 'password', 'password_confirmation']))->roles()->sync($request->roles);

        dd($request);

        // return back()->with('message', 'User has been created successfully.');
}

Any help will be appreciated. Thanks!

0 likes
9 replies
Sinnbeck's avatar

Show the function you use to add them

handleChange

Also. useForm has a post helper

const { post, data, setData, processing, errors, reset } = useForm({
            fname: "",
            lname: "",
            email: "",
            gender: "",
            mobile: "",
            password: "",
            password_confirmation: "",
            roles: [],
        })
//and then
const submit = (e) => {
        e.preventDefault();
       post(route("admin.users.store"));
} 
Emokores's avatar

@Sinnbeck Thank you! Now, this is it:


//This is general for both <Input /> and <Checkbox />
const handleChange = (e) => {
        setData(
            e.target.name,
            e.target.type === "checkbox" ? e.target.checked : e.target.value
        );
};

Sinnbeck's avatar

@Emokores this just seems to overwrite the roles. Try using console.log after checking something off

console.log(data.roles)

Personally I would use a handler specific to roles checkboxes

Emokores's avatar

@Sinnbeck In my console, it returns []. How can I write a handler to handle the checkbox data?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Emokores you need to check if it was checked or unchecked. My examples you get the id of the role

If it was checked you add it

setData('roles', [...data.roles, id])

If it was unchecked you remove it

setData('roles', data.roles.filter((item) =>{
     return item !== id
} )
2 likes
Emokores's avatar

@Sinnbeck Thanks! Damn! It just worked so fine! :) Problem solved. Here's what I've done:


const handleChecked = (e) => {
        let id = e.target.value;
        if (e.target.checked) {
            setData("roles", [...data.roles, id]);
        } else {
            setData(
                "roles",
                data.roles.filter((item) => {
                    return item !== id;
                })
            );
        }
};

2 likes

Please or to participate in this conversation.