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

Emokores's avatar

Display array of selected checkbox fields using React with Inertia in Laravel

I am wondering, how can I display the roles? I have a many-to-many relationship with a role_user table in my backend. If I was using blade files, I'd have something like this in my form:


@foreach ($roles as $role)
     <input type="checkbox" name="roles[]" @isset($user) @if(in_array($role->id, $user->roles->pluck('id')->toArray())) checked @endif @endisset />
@endforeach

But I'm using React. How can I pull in the roles for that specific user?

0 likes
21 replies
Sinnbeck's avatar

Do you have the roles available as an array in inertia? And do the pluck in the method and pass it to inertia as userRoles (or pass it through useForm)

{
    roles.map((role) => {
         return (
          <input type="checkbox" 
              checked={userRoles.includes(role.id)} 
       />
   } 
}

If you have it in useForm

checked={data.roles.includes(role.id)}  
Emokores's avatar

@Sinnbeck This is what I have passed:

const { data, setData, processing, errors, put } = useForm({
            roles: Object.values(roles),
        });

Correct me if I'm wrong

Sinnbeck's avatar

@Emokores close. Pass the already selected roles down

const { data, setData, processing, errors, put } = useForm({
            roles: userRoles
        });
 
Emokores's avatar

@Sinnbeck My problem is how to define userRoles, since I do not have the field in my users_table. I have a users_table, roles_table and role_user_table. So all the roles are defied in the role_user_table by a many-to-many relationship.

Sinnbeck's avatar

@Emokores you actually posted the code yourself

In the array you pass to inertia

'userRoles' => $user->roles->pluck('id'), 
Emokores's avatar

@Sinnbeck It is still not passing the roles. This is what I've done in my UsersController:


public function edit(User $user)
{
        return Inertia::render('Admin/Users/Edit', [
            'roles'  => Role::all(),
            'user' => $user,
            'userRoles' => $user->roles->pluck('id'),
        ]);
  }

This is what I've done in my frontend:


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

Emokores's avatar

@Sinnbeck This is what I get: [3] which I think is the data I passed for this record I've selected. In the object, this is what I get: 0: 3.

Sinnbeck's avatar

@Emokores try adding a console log inside the loop to see if you can find 3

{roles.map((role, index) => {
     return (
        console.log(data.roles, role.id, data.roles.includes(role.id)) 
            <label className="flex items-center" key={role.id}> //fixed. Never use index as key 
                    <Checkbox name="roles[]" id={`role${role.id}`} value={role.id} checked={data.roles.includes(role.id)} handleChange={ handleChecked} required />
                     <span className="ml-2 text-sm text-indigo-600">
                             {role.role}
                      </span>
             </label>
      );
    }
)}
Emokores's avatar

@Sinnbeck I have just created a new record and given the role_id [1, 2]. When I go to the Edit.js and I try to pass other roles, I get [1, 2, '1', '2', '3']. This means the checkboxes are not editing the roles. They're simply adding to what's originally there because the checkboxes are not displaying the checked roles.

Now, when I add console.log(data.roles, role.id, data.roles.includes(role.id)) this is what I get:


(2) [1, 2] 1 true
(2) [1, 2] 2 true
(2) [1, 2] 3 false

Sinnbeck's avatar

@Emokores Based on the console.log() you should have them checked. Is react throwing an error? Does it work if you just set them all to checked={true} ?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Emokores Ok I cannot recreate that... Are your absolutely certain that you are passing it inside the Checkbox component? Or are you throwing it away (you had that issue with value earlier on)

1 like
Emokores's avatar

@Sinnbeck oh my God! It worked. It was something wrong with the component. I threw away the checked values. I missed out checked props in the component. Thanks so much @sinnbeck. You've been helpful in my journey.

Sinnbeck's avatar

@Emokores Great to hear :) Happy to help. Its fun debugging a bit of react. People mostly ask vue related questions when it comes to inertia

1 like
Emokores's avatar

@Sinnbeck That's because Vue is easy to use, especially in Laravel and it was integrated first into Laravel before React. You've helped me a great deal. There's not a lot of content on the web about React with Inertia.

Sinnbeck's avatar

@Emokores True. I had to figure out most of it myself. Luckily I have been using react for a few years before Inertia

1 like
Emokores's avatar

@Sinnbeck And the developer community is lucky to have people like you to help others grow.

Please or to participate in this conversation.