Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8854051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:47:42+00:00 2026-06-14T13:47:42+00:00

When i use sync() method laravel do a lot of separate insert queries in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:47:43+00:00Added an answer on June 14, 2026 at 1:47 pm

    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:

    class User extends \Eloquent 
    {
        public static $timestamps = false;
    
        public function tags()
        {
              return $this->has_many_and_belongs_to('Models\Tag');
        }
    }
    

    Tag model:

    class Tag extends \Eloquent 
    {
        public static $timestamps = false;
    }
    

    How i replaced sync() method

    This is how i forced laravel to do sync() method using multiple insert:

    //$currentUser is a model loaded from database
    //Like this: $currentUser = Auth::user();
    
    $newLinks = array();
    $idsToSync = array();
    
    foreach ($tags as $tag)
    {
        array_push($idsToSync, $tag->id);
    }
    
    //$currentUser->tags()->sync($idsToSync);
    
    $currentIds = $currentUser->tags()->pivot()->lists('tag_id');
    
    $idsToAttach = array_diff($idsToSync, $currentIds);
    
    foreach ($idsToAttach as $value)
    {
        $newLink = array(
                        'user_id' => $currentUser->id,
                        'tag_id' => $value
                         );
    
        $newLinks[] = $newLink;
    }        
    
    if (count($newLinks) > 0)
    {
        \DB::table('tag_user')->insert($newLinks);
    }
    
    $idsToDetach = array_diff($currentIds, $idsToSync);
    
    if (count($idsToDetach) > 0)
    {
        $currentUser->tags()->detach($idsToDetach);
    }
    

    This code does one multiple insert instead of many single ones.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What technique shall one use to implement batch insert/update for Backbone.sync?
I use this method courtesy of casperOne to set property values on form elements
Hi I just ran into a sync problem, and have replicated it in this
i want to use jQuery to sync values of JSF2 components. We are using
I use Eclipse to write Java code and use DropBox to sync my code
When coding, I use Dropbox to sync folder in which I keep my projects
How to use mutex for a database sync between background agent and foreground app?
I'm attempting to use the QuickBooks API to sync up my company's product list
What do you use for data synchronization? I have been working with Microsoft Sync
I have the below LINQ method that I use to create the empty EmploymentPLan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.