1 year ago

Moving Associated Records between Models

Posted 1 year ago by elliotk


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?


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.