title: Laravel - Create and Attach - Many to Many relationship
body: <p>I need some help.</p>
  
  <p>I have these tables: users, buys and codecs. I have a many-to-many relationship: buys, codecs, <strong>buy_codec</strong></p>
  
  <p><strong>Tables</strong></p>
  
  <pre><code>Schema::create('codecs', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;string('name');
      $table-&gt;timestamps();
  });
  
  Schema::create('buys', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;integer('user_id')-&gt;unsigned();
      $table-&gt;string('name');
  });   
  
  Schema::create('buy_codec', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;integer('buy_id')-&gt;unsigned();
      $table-&gt;foreign('buy_id')-&gt;references('id')-&gt;on('buys')-&gt;onDelete('cascade');
  
      $table-&gt;integer('codec_id')-&gt;unsigned();
      $table-&gt;foreign('codec_id')-&gt;references('id')-&gt;on('codecs')-&gt;onDelete('cascade');
  
      $table-&gt;timestamps();
  });    
  </code></pre>
  
  <p><strong>Models</strong></p>
  
  <pre><code>class Codec extends Model
  {
      protected $guarded = ['id'];
      public function buy() {
          return $this-&gt;belongsToMany('App\Buy');
      }
  }
  
  class Buy extends Model
  {
      protected $guarded = ['id'];
      public function codec() {
          return $this-&gt;belongsToMany('App\Codec');
      }
  }
  
  class User extends Authenticatable
  {
      public function buy() {
          return $this-&gt;hasMany('App\Buy');
      }
  }
  </code></pre>
  
  <p>I want to populate the <strong>buy</strong> table with the user_id, and to attach the codecs from the <strong>codecs</strong> table.</p>
  
  <p>This is the <strong>buy</strong> create form.</p>
  
  <pre><code>{!! Form::open(['method'=&gt;'POST', 'action'=&gt;['UserBuyController@store', $usr-&gt;id]]) !!}
  
      &lt;div class="form-group"&gt;
          {!! Form::label('name', 'Name:') !!}
          &lt;div class="input-group"&gt;
              &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-font"&gt;&lt;/i&gt;&lt;/span&gt;
              {!! Form::text('name', null, ['class'=&gt;'form-control']) !!}
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          {!! Form::label('codecs', 'Outbound Codecs:') !!}
          &lt;div class="input-group"&gt;
              &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-language"&gt;&lt;/i&gt;&lt;/span&gt;
              {!! Form::select('codecs[]', $codecs, null, ['class'=&gt;'form-control', 'multiple'=&gt;true]) !!}
          &lt;/div&gt;
      &lt;/div&gt;
  
      {!! Form::submit('Submit', ['class'=&gt;'btn btn-info']) !!}
  
  {!! Form::close() !!}
  </code></pre>
  
  <p>Everything works fine, if i don't attach the codecs.</p>
  
  <p><strong>UserBuyController</strong></p>
  
  <pre><code>class UserBuyController extends Controller
  {
      public function create($userId)
      {
          $codecs = Codec::lists('name', 'id');
          $usr = User::findOrFail($userId);
          return view('buy.create', compact('usr', 'codecs'));
      }
  
      public function store($userId, Request $request)
      {
          $usr = User::findOrFail($userId)-&gt;buy()-&gt;create($request-&gt;all());
          return view('buy.index');
      }
  }
  </code></pre>
  
  <p>How can I create the buy record(because i need the buy id) and than attach the codecs in the pivot table?</p>
  
  <p>Thanks!</p>