elliotk
179
2
Laravel

Moving Associated Records between Models

Posted 9 months ago by elliotk

Hello,

Say I have a Many to Many Relationship between Users and Roles and I wanted to move any Roles from User 1 to User 2 how would I do this?

Something like this?

$user2->roles()->attach($user2->roles()->pluck('id'));

What about if I also had additional data in the pivot? For example

$user1->roles()->->attach($roleId, ['expires' => $expires]);

How would you take the old pivot value and attach that over to the new user, carrying the roles and the pivot values from user1 to user 2?

Thanks for any advice.

Please sign in or create an account to participate in this conversation.