title: Laravel 5 Eloquent many to many pivot with additional attribute
body: <p>I have two tables <code>invoices</code> and <code>items</code> with many to many relationship and I have an additional attribute to their pivot <code>qnty</code>, How do I assign a value to <code>qnty</code> through <code>invoice</code> model using eloquent?
  I am trying to use seeding for testing</p>
  
  <p>My code</p>
  
  <p>Models</p>
  
  <pre><code> class Item extends Model
   {
        public function invoices()
     {
      return $this&gt;belongsToMany('app\Invoice','invoice_item','item_id','invoice_id')-&gt;withPivot('qnty','price');
     }
   }
   class Invoice extends Model
   {
      public function items()
      {
        return $this-&gt;belongsToMany('app\Item','invoice_item','invoice_id','item_id')-&gt;withPivot('qnty','price');
      }
   }
  </code></pre>
  
  <p>The pivot table </p>
  
  <pre><code>Schema::create('invoice_item', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;integer('invoice_id')-&gt;unsigned();
      $table-&gt;foreign('invoice_id')-&gt;references('id')-&gt;on('invoices')-&gt;onDelete('cascade');
      $table-&gt;integer('item_id')-&gt;unsigned();
      $table-&gt;foreign('item_id')-&gt;references('id')-&gt;on('items');
      $table-&gt;integer('qnty')-&gt;unsigned();
      $table-&gt;double('price', 10, 2);
      });
  </code></pre>
  
  <p>And this what I am trying to do in the <code>InvoiceTableSeeder</code></p>
  
  <pre><code> public function run()
  {   $total="";
      DB::table('invoices')-&gt;delete();
       $faker = Faker::create();
      $limit =5;
      for ($i=0; $i &lt; $limit ; $i++) 
      {
          $customer = Customer::all()-&gt;random(1);
          $user = User::all()-&gt;random(1);
          $invoice =  Invoice::create([
              'invoice No' =&gt; $faker-&gt;unique()-&gt;ean8,
              'customer_id' =&gt; $customer-&gt;id,
              'User_id'=&gt; $user-&gt;id,
              ]);
          for ($x=0; $x &lt;10 ; $x++) { 
              $item = Item::all()-&gt;random(1);
              $invoice-&gt;items()-&gt;attach($item-&gt;id);
              $qnt= $invoice-&gt;items()-&gt;pivot-&gt;qnty($faker-&gt;numberBetween($min=1,$max=10));
              $total += $item-&gt;uniteprice *$qnt;
            }
            $invoice-&gt;total = $total;
            $paid= $invoice-&gt;paid= $faker-&gt;numberBetween($min=0,$max=$total);
            $remain= $invoice-&gt;remain= $total-$paid;
            if ($remain&gt;0) {
              $invoice-&gt;pending = true;
              $customer-&gt;account += $remain;  
            }else{
              $invoice-&gt;pending = false;
            }     
      }
  }
  </code></pre>