When i use sync() method laravel do a lot of separate insert queries in my intermediate table like this:
INSERT INTO `tag_user` (`user_id`, `tag_id`) VALUES ('59', '60')
INSERT INTO `tag_user` (`user_id`, `tag_id`) VALUES ('59', '61')
I want it to do one multiple insert like this:
INSERT INTO `tag_user` (`user_id`, `tag_id`) VALUES ('59', '60'), ('59', '61')
Is it possible? I’m using MySql. It would be nice to have attach() method that will accept array as detach() method do. Did someone do this?
This is how i solved it:
My models
In my application each user has many tags(many to many realationship). It’s called toxi database schema.
My user table is called ‘users’, tags table is called ‘tags’. And intermediate table is called ‘tag_user’ that has ‘tag_id’ and ‘user_id’ columns.
User model:
Tag model:
How i replaced
sync()methodThis is how i forced laravel to do
sync()method using multiple insert:This code does one multiple insert instead of many single ones.