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

Emokores's avatar

show many-to-many (roles) in table: Inertia with React

I have a table that shows a list of users. Each user can be assigned more than one role. I would like to display the user roles in the column ROLES.

My backend:

use Inertia\Response as InertiaResponse;

public function index() : InertiaResponse
{
     return inertia('Users/Index', [
         'users' => User::query()->when($search, fn ($query) =>
            $query->where('name', 'LIKE', "%{$search}%")->orWhere('email', 'LIKE', "%{$search}%")
        )->orderByDesc('created_at')->paginate(10)->withQueryString(),
         'roles' => Role::all(),
     ]);
}

In my frontend:


const { users, roles } = usePage().props, { data } = users;

return (
  {data.map((user, index) => (
      <Table>
         <tr key={index}>
           <td>{user.name}</td>
           <td>{/* TODO: userRoles */}</td>
         </tr>
      </Table>
   ))}
);

I'm stuck on how to bring in to display the user roles for each user. I know I have to first declare it in the backend, then pass it to React to render it. But how??

0 likes
1 reply
Emokores's avatar
Emokores
OP
Best Answer
Level 2

I actually solved this and displayed the roles in a modal. I set the Objectives.values(user.roles) array in the setState({roles: []}) and mapped them over using state.roles.map() to output the values.

Please or to participate in this conversation.