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

GTHell's avatar

How do I assign my users to existed roles in many to many relationship?

Hi, I'm new to Laravel.

I have Users and Roles with an intermediate role_user table. Both of the Users and Roles table have data but how do I assign user to any role using eloquent?

The below code doesn't work and I can't use the save() also.

Route::get('/assign', function(){
    $user = User::find(1);
    $role = $user->roles;
    $role->id='2';
});

Is there anyway to assign user to some role without going into dbms?

0 likes
1 reply

Please or to participate in this conversation.