Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how i solved it:</p> <h2>My models</h2> <p>In my application each user has many tags(many to many realationship). It's called <a href="http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html" rel="nofollow">toxi</a> 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.</p> <p>User model:</p> <pre><code>class User extends \Eloquent { public static $timestamps = false; public function tags() { return $this-&gt;has_many_and_belongs_to('Models\Tag'); } } </code></pre> <p>Tag model:</p> <pre><code>class Tag extends \Eloquent { public static $timestamps = false; } </code></pre> <h2>How i replaced <code>sync()</code> method</h2> <p>This is how i forced laravel to do <code>sync()</code> method using multiple insert:</p> <pre><code>//$currentUser is a model loaded from database //Like this: $currentUser = Auth::user(); $newLinks = array(); $idsToSync = array(); foreach ($tags as $tag) { array_push($idsToSync, $tag-&gt;id); } //$currentUser-&gt;tags()-&gt;sync($idsToSync); $currentIds = $currentUser-&gt;tags()-&gt;pivot()-&gt;lists('tag_id'); $idsToAttach = array_diff($idsToSync, $currentIds); foreach ($idsToAttach as $value) { $newLink = array( 'user_id' =&gt; $currentUser-&gt;id, 'tag_id' =&gt; $value ); $newLinks[] = $newLink; } if (count($newLinks) &gt; 0) { \DB::table('tag_user')-&gt;insert($newLinks); } $idsToDetach = array_diff($currentIds, $idsToSync); if (count($idsToDetach) &gt; 0) { $currentUser-&gt;tags()-&gt;detach($idsToDetach); } </code></pre> <p>This code does one multiple insert instead of many single ones.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload