title: How to add extra column to pivot table many to many
body: <p>I have simple <code>items</code> table.</p>
  <p>Example:</p>
  <p>Paper. Made from  bamboo and cotton  (if google does not lie).</p>
  <p>Also, a book made from paper.</p>
  <p>So paper parent of bamboo and cotton  also child of book.</p>
  <p>I think I can explain my table structure in two words.</p>
  <p>And now my code. All of this code examples and table structures simplified as much as possible.</p>
  <p><code>items</code></p>
  <pre><code>╔════╦═══════════════╦══════╗
  ║ id ║  name         ║ price║
  ╠════╬═══════════════╬══════╣
  ║  1 ║ Book          ║ 500  ║
  ║  2 ║ Paper         ║  50  ║
  ║  3 ║ Bamboo        ║  15  ║
  ║  4 ║ Cotton        ║  7   ║
  ╚════╩═══════════════╩══════╝
  </code></pre>
  <p><code>item_item</code> table (many to many)</p>
  <pre><code>╔════╦═══════════════╦══════════╗
  ║ id ║  item_id      ║ parent_id║
  ╠════╬═══════════════╬══════════╣
  ║  1 ║     2         ║    1     ║
  ║  2 ║     3         ║    2     ║
  ║  3 ║     4         ║    2     ║
  ╚════╩═══════════════╩══════════╝
  </code></pre>
  <p>Model <code>Item</code></p>
  <pre><code>class Item extends Model
  {
      protected $fillable = [&quot;name&quot;, &quot;price&quot;];
  
      public $timestamps = false;
  
      public function components()
      {
          return $this-&gt;belongsToMany('App\Models\Item', &quot;item_item&quot;, &quot;parent_id&quot;, &quot;item_id&quot;);
      }
  }
  </code></pre>
  <p><strong>ItemController</strong> create</p>
  <pre><code> public function store(Request $request)
      {
          $request-&gt;validate([
              &quot;name&quot; =&gt; &quot;required&quot;,
              &quot;price&quot; =&gt; &quot;required&quot;,
          ]);
  
          $item = Item::create([
              &quot;name&quot; =&gt; $request-&gt;name,
              &quot;price&quot; =&gt; $request-&gt;price,
          ]);
  
          $item-&gt;components()-&gt;attach($request-&gt;items);
  
  
          return redirect()-&gt;route(&quot;items&quot;);
      }
  </code></pre>
  <p>Update with sync:</p>
  <pre><code>$item-&gt;components()-&gt;sync($request-&gt;components);
  </code></pre>
  <p>And my view</p>
  <pre><code>&lt;div class=&quot;form-group&quot;&gt;
      &lt;label for=&quot;item&quot;&gt;Choose child items&lt;/label&gt;
      @foreach ($items as $item)
          &lt;div class=&quot;checkbox&quot;&gt;
              &lt;label &gt;&lt;input class=&quot;mr-2&quot; name=&quot;items[]&quot; type=&quot;checkbox&quot; value=&quot;{{ $item-&gt;id }}&quot;/&gt;{{ $item-&gt;name }}&lt;/label&gt;
          &lt;/div&gt;
      @endforeach
  &lt;/div&gt;
  </code></pre>
  <p>This is work fine, but the question is how to add column <code>item_count</code> to pivot table <code>item_item</code>. For count how many items need to be create something.</p>
  <p>For example 500 papers to create a book</p>
  <p>I try to do like in <a href="https://www.youtube.com/watch?v=v6kX9U5xxqQ" rel="nofollow noreferrer">this</a> video  but doesn't work.</p>