title: Many to many insertion error Call to a member function Laravel 5.5
body: <p>I have a trouble in many to many insertion. My code in controller is right. After I add the two data which is successful, the third time I try to insert produce an error:</p>
  
  <blockquote>
    <p>Call to a member function bagcollects() on null</p>
  </blockquote>
  
  <p>and the error is on this code </p>
  
  <pre><code>$collection-&gt;bagcollects()-&gt;attach($bagcollect-&gt;id);
  </code></pre>
  
  <p>I just don't understand why this error occurred!</p>
  
  <p>I'll show the whole code of CollectionsController store method:</p>
  
  <pre><code>public function addbag(Request $request){
      $collection = Collection::find($request-&gt;input('collection_id'));
      $bagcollect = Bagcollect::create([
          'bag_id' =&gt; $request-&gt;input('bag_id'),
          'weight' =&gt; $request-&gt;input('weight')
      ]);
      $collection-&gt;bagcollects()-&gt;attach($bagcollect-&gt;id);
      return redirect()-&gt;route('collections.show', ['collection'=&gt; $collection-&gt;id]);
  }
  </code></pre>
  
  <p>Migration on collections</p>
  
  <pre><code>Schema::disableForeignKeyConstraints();
  if(!Schema::hasTable('collections')){
      Schema::create('collections', function (Blueprint $table) {
          $table-&gt;engine = "InnoDB";
          $table-&gt;increments('id');
          $table-&gt;integer('assignment_id')-&gt;unsigned();
          $table-&gt;foreign('assignment_id')-&gt;references('id')-&gt;on('assignments')-&gt;onUpdate('cascade')-&gt;onDelete('cascade');
          $table-&gt;timestamp('collected_on');
      });
  }
  </code></pre>
  
  <p>Migration on bagcollects</p>
  
  <pre><code>Schema::create('bagcollects', function (Blueprint $table) {
      $table-&gt;engine = "InnoDB";
      $table-&gt;increments('id');
      $table-&gt;integer('bag_id')-&gt;unsigned();
      $table-&gt;double('weight', 8, 2);
      $table-&gt;foreign('bag_id')-&gt;references('id')-&gt;on('bags');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p>Migration bagcollect_collection</p>
  
  <pre><code>Schema::create('bagcollect_collection', function (Blueprint $table) {
      $table-&gt;engine = "InnoDB";
      $table-&gt;increments('id');
      $table-&gt;integer('bagcollect_id')-&gt;unsigned();
      $table-&gt;integer('collection_id')-&gt;unsigned();
      $table-&gt;foreign('bagcollect_id')-&gt;references('id')-&gt;on('bagcollects');
      $table-&gt;foreign('collection_id')-&gt;references('id')-&gt;on('collections');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p>collections show.blade.php add modal</p>
  
  <pre><code>&lt;!-- ADD MODAL --&gt;
  &lt;div class="modal fade" role="dialog" id="addModal"&gt;
      &lt;div class="modal-dialog"&gt;
          &lt;div class="modal-content"&gt;
              &lt;div class="modal-header"&gt;
                  &lt;button type="button" class="close" data-dismiss="modal"&gt;&amp;times;&lt;/button&gt;
                  &lt;h4 class="modal-title"&gt;New Collection&lt;/h4&gt;
              &lt;/div&gt;
  
              &lt;div class="modal-body"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-12"&gt; 
                          &lt;form class="form-horizontal" method="POST" action="{{ route('collections.addbag') }}"&gt;
                              {{ csrf_field() }}
  
  
                              &lt;div class="row form-group"&gt;  
  
                                  &lt;input class="form-control" name = "collection_id" id="collection_id" value="{{$collection-&gt;id}}" type="hidden"&gt;
                                   &lt;div class="{{ $errors-&gt;has('bag') ? ' has-error' : '' }}"&gt;
                                          &lt;div class="col-md-8"&gt;
                                              &lt;label for="bag_id"&gt;Bag&lt;/label&gt;
                                               &lt;select class="form-control" required id="bag" name="bag_id"&gt;
                                                   &lt;option value="" data-hidden="true"  
                                                      selected="selected"&gt;
                                                  &lt;/option&gt;
                                                      @foreach($bags as $bag)
                                                          &lt;option value= "{{ $bag-&gt;id }}"&gt;
                                                                {{ $bag-&gt;name }} 
                                                          &lt;/option&gt;
                                                      @endforeach
                                                &lt;/select&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
  
                                    &lt;div class="{{ $errors-&gt;has('weight') ? ' has-error' : '' }}"&gt;
                                          &lt;div class="col-md-8"&gt;
                                              &lt;label for="weight"&gt;Weight&lt;/label&gt;
                                               &lt;input type="text" class="form-control" id="weight" name= "weight" required&gt;
                                          &lt;/div&gt;
                                    &lt;/div&gt;
  
                              &lt;/div&gt;   
  
                              &lt;!-- SUBMIT BUTTON --&gt;
                              &lt;button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add"&gt;
                                  Create
                              &lt;/button&gt;
  
                              &lt;button  data-dismiss="modal" aria-hidden="true" class="btn btn-basic pull-right" style="margin-right: 2%"&gt;
                                  Cancel
                              &lt;/button&gt;             
                              &lt;div class="clearfix"&gt;&lt;/div&gt;
  
                          &lt;/form&gt;                
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Collection.php model</p>
  
  <pre><code>protected $fillable = [
      'id',
      'assignment_id',
      'bag_id'
  ];
  
  public $timestamps = false;
  
  public function assignment()
  {
      return $this-&gt;belongsTo('App\Assignment');
  }
  
  public function bagcollects()
  {
      return $this-&gt;belongsToMany('App\Bagcollect');
  }
  </code></pre>
  
  <p>Bagcollect.php</p>
  
  <pre><code>protected $fillable = [
      'bag_id',
      'weight'
  ];
  
  public function collections()
  {
      return $this-&gt;belongsToMany('App\Collection');
  }
  
  public function bag()
  {
      return $this-&gt;belongsTo('App\Bag');
  }
  </code></pre>
  
  <p>BagcollectCollection.php</p>
  
  <pre><code>protected $table = "bagcollect_collection";
  
  protected $fillable = [
      'bagcollect_id',
      'collection_id',
  
  ];
  </code></pre>