Mysql - What would be the pure sql query that would be equivalent to Laravel Sync in a pivot table?

Asked

Viewed 39 times

0

When I work with Laravel in a pivot table where we have only the fields user_id and post_id we use something similar to $user->posts()->sync([3,4]) and the pivot table will be updated with posts 3 and 4 for the user in question.

If this user before this operation had posts 3,8 and 9, at the end of the operation he will have only posts 3 and 4.

In another project where I don’t use Laravel what would be the equivalent query to this operation with Sync? In other words, it would be something that deleted the post 8 and 9, kept the 3 and inserted the 4.

You can do this in one query?

  • 1

    So, Sync does two delete and Insert operations and if I’m not mistaken select to see the existing items. Summarizing there is no way to do in an SQL

  • 1

    Enable the debug on the Laravel to get the sql logs that you can get the exact query.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.